笔者最近开发的系统中需要写一个下载文件的功能。以前用BCB调用API写的很烦琐,忽然想起有一个API就可以搞定了,于是一大早就来搜索。这个API就是UrlDownloadToFile。不仅如此,Delphi的一些控件也可以轻松实现下载,如NMHTTP,指定NMHTTP1.InputFileMode := ture; 指定Body为本地文件名,指定Get就可以下载了。下面是详细代码,均出自CSDN。我把它们都整理到这儿,让大家方便查阅。
|
|
========================
例程:
|
PChar(''),nil,SW_NORMAL) |
=================
加入如下代码:
|
|
试试吧,Delphi的目录中有TNMHTTP控件的例子。NT4+,Win95+,IE3+,你可以用URL Moniker的功能。
|
|
其中最后一个参数你还可以传入一个IBindStatusCallback的实现以跟踪下载进度或控制中止下载。简单的场合一句话就搞定了。
BTW, URL Moniker封装了大多数URL,而不是像NMHTTP那样封装协议,因此你可以用URLDownloadToFile下载HTTP,FTP甚至本地文件和局域网文件,还有其他的custom moniker,比如MSITSTORE(MSDN Library的文档moniker实现)。
|
|
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/ ]