您的位置:首页 > 编程语言 > Delphi

Delphi组件Indy 10中修正IdFTP不能续传问题

2007-12-07 09:35 381 查看
原文:http://blog.dream4dev.com/article.asp?id=144

提出问题:
在使用IdFTP组件下载文件时候,发现不能续传文件
procedure Get(const ASourceFile: string; ADest: TIdStream; AResume: Boolean = false); overload;
procedure Get(const ASourceFile, ADestFile: string; const ACanOverwrite: boolean = false; AResume: Boolean = false); overload;

查看源代码(IdFTP.pas),发现:

procedure TIdFTP.Get(const ASourceFile, ADestFile: string; const ACanOverwrite: boolean = False;
AResume: Boolean = false);
var
LDestStream: TIdStream;
begin
.....
try
Get(ASourceFile, LDestStream, AResume);//---->主要用到TIdFTP.Get(const ASourceFile: string; ADest: TIdStream; AResume: Boolean = False);
finally
Sys.FreeAndNil(LDestStream);
end;
end;

procedure TIdFTP.Get(const ASourceFile: string; ADest: TIdStream; AResume: Boolean = False);
begin
//for SSL FXP, we have to do it here because InternalGet is used by the LIST command
//where SSCN is ignored.
ClearSSCN;
AResume := AResume and CanResume;
ADest.Position := 0;//---->问题出在这句,每次都重置了。
InternalGet('RETR ' + ASourceFile, ADest, AResume);
end;
为什么这句会使控件不能续传,因为续传命令代码: procedure InternalGet(const ACommand: string; ADest: TIdStream; AResume: Boolean = false);
.......
if AResume then begin
Self.SendCmd('REST ' + Sys.IntToStr(ADest.Position), [350]); {do not localize}
end;
解决办法:
修改过程procedure TIdFTP.Get(const ASourceFile: string; ADest: TIdStream; AResume: Boolean = False);
只要Remark了 ADest.Position := 0;问题就能解决
但这样做,也有后遗症,每次执行Get之前,还需要指定Position,否则容易出问题(打开Stream时候,默认Posotion为0)

完美解决:
把ADest.Position := 0;
修改为ADest.Position := ADest.Size;

完整代码: procedure TIdFTP.Get(const ASourceFile: string; ADest: TIdStream; AResume: Boolean = False);
begin
//for SSL FXP, we have to do it here because InternalGet is used by the LIST command
//where SSCN is ignored.
ClearSSCN;
AResume := AResume and CanResume;
if AResume then//Fix by Dream4Drv
ADest.Position := ADest.Size
else
ADest.Position := 0;
InternalGet('RETR ' + ASourceFile, ADest, AResume);
end;
Delphi7~Delphi2007组件Indy 10的IdFTP中均存在同样问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: