【发布时间】:2015-07-16 13:17:41
【问题描述】:
- Q1:为什么在下载过程中无法关闭模态表单
- Q2:为什么下载完成后进度条没有100%丰富(是重绘的一种方式?)
- Q3:为什么如果我停止并重新启动与 Web 服务器的连接 下载传输正在停止且未显示任何错误,并且 永远不要继续?我该怎么做才能获取并捕获错误并继续 回到初始状态(下载并安装进度条在 位置 0)
备注:IdAntiFreeze 已激活
procedure Tform_update.button_downloadClick(Sender: TObject);
var
FS: TFileStream;
url, file_name: String;
begin
//execute download
if button_download.Tag = 0 then
begin
Fdone:= False;
Fcancel:= False;
url:= APP_DOMAIN + '/downloads/Setup.exe';
file_name:= 'C:\Temp\Setup.exe';
if FileExists(file_name) then DeleteFile(file_name);
try
FS:= TFileStream.Create(file_name, fmCreate);
Http:= TIdHTTP.Create(nil);
Http.OnWorkBegin:= HttpWorkBegin;
Http.OnWork:= HttpWork;
Http.Get(url, FS);
finally
FS.Free;
Http.Free;
if Fdone then ModalResult:= mrOk;
end;
end
else
//cancel download
begin
Fcancel:= True;
end;
end;
procedure Tform_update.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
ContentLength: Int64;
Percent: Integer;
begin
ContentLength:= Http.Response.ContentLength;
if AWorkCount = ContentLength then Fdone:= True; //
if (Pos('chunked', LowerCase(Http.Response.TransferEncoding)) = 0) and (ContentLength > 0) then
begin
sleep(15);
Percent := 100 * AWorkCount div ContentLength;
progress_bar.Position:= Percent;
end;
//stop download
if Fcancel and Http.Connected then
begin
Http.IOHandler.InputBuffer.Clear;
Http.Disconnect;
Fcancel:= False;
button_download.Caption:= _('Download and Install');
button_download.Tag:= 0;
progress_bar.Position:= 0;
end;
end;
procedure Tform_update.HttpWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
if AWorkMode <> wmRead then Exit;
button_download.Tag:= 1;
button_download.Caption:= _('Cancel');
end;
procedure Tform_update.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Fcancel:= True;
end;
【问题讨论】:
-
您显然错过了这个网站的运作方式。这是一个问答网站(单数),而不是Multiple Questions and Answers(复数)。帖子应包含一个问题。如果您有多个帖子,请创建单独的帖子;您可以随时链接回一个或多个其他人以提供参考资料。
-
感谢大家的快速回复!所有答案都有帮助。我会用线程来做。 @KenWhite 我会记住下一个问题。
标签: delphi download indy idhttp