新建一个工程,放上一个TIdHTTP控件,一个TIdAntiFreeze控件,一个TProgressBar用于显示下载进度。最后放上一个TButton用于开始执行我们的命令。代码如下:
procedure TForm1.Button2Click(Sender: TObject); var MyStream:TMemoryStream; begin IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应. MyStream:=TMemoryStream.Create; try //下载我站点的一个ZIP文件 IdHTTP1.Get('http://www.138soft.com/download/Mp3ToExe.zip',MyStream); except//INDY控件一般要使用这种try..except结构. Showmessage('网络出错!'); MyStream.Free; Exit; end; MyStream.SaveToFile('c:\Mp3ToExe.zip'); MyStream.Free; Showmessage('OK'); end;
procedure TForm1.IdHTTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode; const AWorkCountMax: Integer); begin ProgressBar1.Max:=AWorkCountMax; ProgressBar1.Min:=0; ProgressBar1.Position:=0; end;
|
IdHTTP1的Get还有一种形式就是获取字符串:例如,上面的程序可以改写成:
procedure TForm1.Button1Click(Sender: TObject); var MyStr:String; begin IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应. try MyStr:=IdHTTP1.Get('http://www.138soft.com/default.htm'); except Showmessage('网络出错!'); Exit; end; Showmessage(MyStr); end; |
关键词:用 Delphi 下载网