zoukankan      html  css  js  c++  java
  • 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函数中用参数来处理自已的进度条以显示下载进度。因为我的程序不需要,所以没有写出来。
  • 相关阅读:
    L1-031 到底是不是太胖了
    L1-030 一帮一
    PyCharm--git配置
    websocket--python
    UDP--python
    TCP--python
    pytest--metadata
    pytest--xdist
    pytest--夹具
    pytest--变量
  • 原文地址:https://www.cnblogs.com/taobataoma/p/717174.html
Copyright © 2011-2022 走看看