Delphi从Internet下载文件

 今天在做拍卖系统的时候,因考虑到网络状况问题,需要将拍品所有信息下载到本机,包括拍品图片,因此需要实现从Internet下载文件的功能。

      下面是代码片段:
 1 ..
 2 
 3   private
 4     function DownloadFile(SourceFile, DestFile: string): Boolean;
 5     procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
 6           Progress, ProgressMax: Cardinal;
 7           StatusCode: TURLDownloadStatus;
 8           StatusText: String; var Cancel: Boolean) ;
 9     { Private declarations }
10 
11 .
12 
13 function TFrameChannel.DownloadFile(SourceFile, DestFile: string): Boolean;
14 var
15   hasError: boolean;
16 begin
17   hasError:=false;
18   with TDownloadURL.Create(self) do
19   try
20     URL:=SourceFile;
21     FileName := DestFile;
22     OnDownloadProgress := URL_OnDownloadProgress;
23     ExecuteTarget(nil) ;
24   except on e: Exception do begin
25     FormMain.SetStatusInfo(e.Message);
26     Free;
27     hasError:=true;
28     end;
29   end;
30   Result := not hasError;
31 end;
32 
33 procedure TFrameChannel.URL_OnDownloadProgress;
34 begin
35    Application.ProcessMessages;
36 end;
37 
38 调用时:
39     ofname:='http://www.aaa.com/1.jpg';
40     nfname:='images\1.jpg';
41     if not DownloadFile(ofname, nfname) then 
42       showMessage('Error')
43     end
44       showMessage('OK');
45 


      注:以上程序在大文件下载过程不会死锁,而且你可以在URL_OnDownloadProgress函数中用参数来处理自已的进度条以显示下载进度。因为我的程序不需要,所以没有写出来。

http://www.cnblogs.com/taobataoma/archive/2007/04/17/717174.html

原文地址:https://www.cnblogs.com/sunsoft/p/1965635.html