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函数中用参数来处理自已的进度条以显示下载进度。因为我的程序不需要,所以没有写出来。

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

  • 相关阅读:
    [Cloud Architect] 12. Defensive Security in the Cloud
    [SAP] 38. Database Migration Service
    [Cloud Architect] 11. Protecting Data Stored in the Cloud
    [SAP] 37. Snow family
    [SAP] 36. Storage getway
    JAVA开发常见问题整理(持续更新)
    sdf1434 最少转弯
    sdf 2439 问题 A: 迷宫(广搜模板题)
    sdf1552
    小学生数据结构和基础算法
  • 原文地址:https://www.cnblogs.com/sunsoft/p/1965635.html
Copyright © 2011-2022 走看看