在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;
|
请发表评论