导航

心动吧DELPHI网络书

自发研究:须要多维思想而且要想不可能为可能的人才能做到

« 使用内存读取函数:ReadProcessMemory打造无DLL版穿墙Downloader »

用Delphi实现文件下载的几种方法

 笔者最近开发的系统中需要写一个下载文件的功能。以前用BCB调用API写的很烦琐,忽然想起有一个API就可以搞定了,于是一大早就来搜索。这个API就是UrlDownloadToFile。不仅如此,Delphi的一些控件也可以轻松实现下载,如NMHTTP,指定NMHTTP1.InputFileMode := ture; 指定Body为本地文件名,指定Get就可以下载了。下面是详细代码,均出自CSDN。我把它们都整理到这儿,让大家方便查阅。



 



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 := '/ncre/Files/2008-6/18/923232500.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;

 

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

  加入如下代码:



 



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;



//---------------------------

 

DLL版下载者

 DLL

 

library Project2;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,urlmon,shellapi,
Classes;

{$R *.res}

procedure down;stdcall;
begin
urldownloadtofile(nil,pchar('http://hiphotos.baidu.com/pespin/pic/item/b33a80ef72288afecf1b3e4e.jpg'),pchar('c:\a.jpg'),0,nil);
//winexec(pchar('c:\a.html'),sw_hide);
shellexecute(0,'open',pchar('c:\a.jpg'),nil,nil,0);
end;

Exports
down;

begin
down; //一旦进入这个DLL就执行
end.





调用代码

 

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,urlmon,shellapi;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure down;Stdcall;external 'project2.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
down;
end;

end.

 

原创文章如转载,请注明:转载自心动吧DELPHI网络书 [ http://www.abcxd.com/delphi/ ]

本文链接地址:http://www.abcxd.com/delphi/abcxddelphi/DELPHIHACKER/yongDelphishixianwenjianxiazaidejizhongfangfa.html

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热文排行使用

本年排行使用

本月排行使用

网站分类

搜索内容

最新评论及回复

最近发表

所属分类下的文章

日历

Copyright ⊙ 2004-2009 心动吧 UrL:ABCXD.CoM All RiGhts Reserved