zoukankan      html  css  js  c++  java
  • 用Delphi实现文件下载的几种方法(三种使用控件的方法)

    有个API就是UrlDownloadToFile。不仅如此,Delphi的一些控件也可以轻松实现下载,如NMHTTP,指定NMHTTP1.InputFileMode := ture; 指定Body为本地文件名,指定Get就可以下载了。

    uses UrlMon; 
    function DownloadFile(Source, Dest: string): Boolean; 
    begin 
      try 
        Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0; 
        except 
          Result := False; 
        end; 
      end; 
      
      if DownloadFile(''http://www.borland.com/delphi6.zip, ''c:kylix.zip'') then 
    ShowMessage(''Download succesful'') 
    else ShowMessage(''Download unsuccesful'') 




    ======================== 
    例程: 



    Uses URLMon, ShellApi; 
    function DownloadFile(SourceFile, DestFile: string): Boolean; 
    begin 
    try 
    Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0; 
    except 
    Result := False; 
    end; 
    end; 

    procedure TForm1.Button1.Click(Sender: TObject); 
    const 
    // URL Location 
    SourceFile := ''http://www.google.com/intl/de/images/home_title.gif''; 
    // Where to save the file 
    DestFile := ''c: empgoogle-image.gif''; 
    begin 
      if DownloadFile(SourceFile, DestFile) then 
      begin 
        ShowMessage(''Download succesful!''); 
        // Show downloaded image in your browser 
    ShellExecute(Application.Handle,PChar(''open''),PChar(DestFile),PChar(''''),nil,SW_NORMAL) 
      end 
      else 
      ShowMessage(''Error while downloading '' + SourceFile) 
    end; 




    ================= 

    加入如下代码: 



    NMHTTP1.InputFileMode := ture; 
    NMHTTP1.Body := ''本地文件名''; 
    NMHTTP1.Header := ''Head.txt''; 
    NMHTTP1.OutputFileMode := FALSE; 
    NMHTTP1.ReportLevel := Status_Basic; 
    NMHTTP1.Proxy := ''代理服务器的IP地址''; 
    NMHTTP1.ProxyPort := ''代理服务器的端口号''; 
    With NMHTTP1.HeaderInfo do 
      
      Begin 
        Cookie := ''''; 
        LocalMailAddress := ''''; 
        LocalProgram := ''''; 
        Referer := ''''; 
        UserID := ''用户名称''; 
        Password := ''用户口令''; 
        End; 
        
        NMHTTP1.Get(‘http://www.abcdefg.com/software/a.zip''); 




    试试吧,Delphi的目录中有TNMHTTP控件的例子。NT4+,Win95+,IE3+,你可以用URL Moniker的功能。 



    uses URLMon; 

    ... 

    OleCheck(URLDownloadToFile(nil,''URL'',''Filename'',0,nil)); 




    其中最后一个参数你还可以传入一个IBindStatusCallback的实现以跟踪下载进度或控制中止下载。简单的场合一句话就搞定了。 

    BTW, URL Moniker封装了大多数URL,而不是像NMHTTP那样封装协议,因此你可以用URLDownloadToFile下载HTTP,FTP甚至本地文件和局域网文件,还有其他的custom moniker,比如MSITSTORE(MSDN Library的文档moniker实现)。 




    var 
    DownLoadFile:TFileStream; 
    beginio 
    DownLoadFile:=TFileStream.Create(''c:aa.rar'',fmCreate); 
    IdHTTP1.Get(''http://www.sina.com.cn/download/aa.rar'',DownLoadFile); 
    DownLoadFile.Free; 
    end; 

    http://blog.csdn.net/zang141588761/article/details/51934072

  • 相关阅读:
    枚举、函数关于oracle函数listagg的使用说明by小雨
    执行、Mongodb MapReduce示例1个by小雨
    事务、异常TSQL 编码时应该注意的10个问题by小雨
    源、执行GoldenGate 单向DDL同步by小雨
    Oracle中的所有权限by小雨
    数据库、版本数据库学习从此开始by小雨
    统计、案例深入理解Oracle索引(10):索引列字符类型统计信息的32位限制by小雨
    字段、数据库表三大范式及存储方式by小雨
    数据库、用户第二章Getting Start with the Oracle Server(oracle入门)by小雨
    搜索、关键字截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果by小雨
  • 原文地址:https://www.cnblogs.com/findumars/p/6711264.html
Copyright © 2011-2022 走看看