http文件下載

http://www.enet.com.cn/eschool/inforcenter/A20040429306316.html
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ShellApi,URLMon;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function DownloadFile(Source, Dest: string): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if DownloadFile('http://cc.hbu.edu.cn/jackfan/wbx.exe', 'c:/rd.exe') then
    ShowMessage('Download succesful')
  else ShowMessage('Download unsuccesful');
end;

function TForm1.DownloadFile(Source, Dest: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
  except
    Result := False;
  end;
end;

end.

例子二:

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:/temp/google-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; 

還可以用NMHTTP控件
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的實現以跟蹤下載進度或控制中止下載。簡單的場合一句話就搞定了。

  --回覆得分 0-- 

  BTW, URL Moniker封裝了大多數URL,而不是像NMHTTP那樣封裝協議,因此你可以用URLDownloadToFile下載HTTP,FTP甚至本地文件和局域網文件,還有其他的custom moniker,比如MSITSTORE(MSDN Library的文檔moniker實現)。 

  用IdHTTP控件吧!

  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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章