下载主函数
procedure TForm1.HttpDownLoad(aURL, aFile: string; bResume: Boolean);
var
tStream: TFileStream;
begin //Http方式下载
if FileExists(aFile) then //如果文件已经存在
tStream := TFileStream.Create(aFile, fmOpenWrite) else
tStream := TFileStream.Create(aFile, fmCreate); if bResume then //续传方式
begin
IdHTTP1.Request.ContentRangeStart := tStream.Size - 1;
tStream.Position := tStream.Size - 1; //移动到最后继续下载
IdHTTP1.Head(aURL);
IdHTTP1.Request.ContentRangeEnd := IdHTTP1.Response.ContentLength;
end else //覆盖或新建方式
begin
IdHTTP1.Request.ContentRangeStart := 0;
end; try
IdHTTP1.Get(aURL, tStream); //开始下载
finally
tStream.Free;
end;
end;暂停按钮
procedure TForm1.Button2Click(Sender: TObject);
begin
AbortTransfer := True;
end;当点击暂停按钮后,再点击下载,能够实现文件的断点下载,一切功能正常
但是当我直接结束程序后,虽然文件还没有下载完,但是已经下载文件的大小和实际大小一样,再点击下载,文件的大小就是实际大小的两倍了,有没有什么其它的解决办法
procedure TForm1.HttpDownLoad(aURL, aFile: string; bResume: Boolean);
var
tStream: TFileStream;
begin //Http方式下载
if FileExists(aFile) then //如果文件已经存在
tStream := TFileStream.Create(aFile, fmOpenWrite) else
tStream := TFileStream.Create(aFile, fmCreate); if bResume then //续传方式
begin
IdHTTP1.Request.ContentRangeStart := tStream.Size - 1;
tStream.Position := tStream.Size - 1; //移动到最后继续下载
IdHTTP1.Head(aURL);
IdHTTP1.Request.ContentRangeEnd := IdHTTP1.Response.ContentLength;
end else //覆盖或新建方式
begin
IdHTTP1.Request.ContentRangeStart := 0;
end; try
IdHTTP1.Get(aURL, tStream); //开始下载
finally
tStream.Free;
end;
end;暂停按钮
procedure TForm1.Button2Click(Sender: TObject);
begin
AbortTransfer := True;
end;当点击暂停按钮后,再点击下载,能够实现文件的断点下载,一切功能正常
但是当我直接结束程序后,虽然文件还没有下载完,但是已经下载文件的大小和实际大小一样,再点击下载,文件的大小就是实际大小的两倍了,有没有什么其它的解决办法
begin
IdHTTP1.Request.ContentRangeStart := tStream.Size - 1;
tStream.Position := tStream.Size - 1; //移动到最后继续下载
IdHTTP1.Head(aURL);
IdHTTP1.Request.ContentRangeEnd := IdHTTP1.Response.ContentLength;
end else //覆盖或新建方式
begin
IdHTTP1.Request.ContentRangeStart := 0;
end;
可能问题是出在bResume这上面你跟踪一下试试好像你再按一下一载就把指针指到0了
IdHTTP1.Request.ContentRangeStart := 0;