大悟还俗

邮箱 key_ok@qq.com 我的收集 http://pan.baidu.com/share/home?uk=1177427271
posts - 236, comments - 8, trackbacks - 0, articles - 0
  新随笔  :: 联系 :: 订阅 订阅  :: 管理

Delphi IdHTTP1下载文件防止假死 (

Posted on 2014-01-25 16:18 大悟还俗 阅读(...) 评论(...) 编辑 收藏

在Form1中添加控件:
两个Indy控件:IdAntiFreeze1,IdHTTP1;
一个按钮 :Button1;
一个进度条 :ProgressBar1 显示下载速度

procedure TForm1.Button1Click(Sender: TObject);
var
MyStream:TMemoryStream;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
MyStream:=TMemoryStream.Create;
try
//http://www.delphitop.com/
IdHTTP1.Get('http://www.appcontrols.com/demos/exe/HTTPDemo.exe',MyStream);
except
Showmessage('没有下载成功!');
MyStream.Free;
Exit;
end;
MyStream.SaveToFile(ExtractFilePath(paramstr(0))+'HTTPDemo.zip');
MyStream.Free;
Showmessage('OK');
end;

procedure TForm1.IdHTTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
begin
//开始下载前,将ProgressBar1的最大值设置为需要接收的数据大小.
ProgressBar1.Max:=AWorkCountMax;
ProgressBar1.Min:=0;
ProgressBar1.Position:=0;
end;

procedure TForm1.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
//接收数据的时候,进度将在ProgressBar1显示出来.
ProgressBar1.Position:=ProgressBar1.Position+AWorkCount;
end;