Delphi程序運行於Windows平台,利用Windows平台的API可實現很多功能,今天這個調用Windows API下載文件的例子就是其中之一,調用win的API可簡化我們的Delphi代碼,必竟我們要依靠微軟嘛,所以有興趣的朋友,不妨參考下下面這個Delphi下載文件的代碼:
01
unit Unit1;
02
interface
03
uses
04
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
05
Dialogs, WinInet, StdCtrls;
06
type
07
TForm1 = class(TForm)
08
Button1: TButton;
09
Memo1: TMemo;
10
Button2: TButton;
11
procedure Button1Click(Sender: TObject);
12
procedure Button2Click(Sender: TObject);
13
private
14
{ Private declarations }
15
public
16
{ Public declarations }
17
end;
18
var
19
Form1: TForm1;
20
implementation
21
{$R *.dfm}
22
function DownloadToString(const Url: string): string;
23
var
24
NetHandle: HINTERNET;
25
UrlHandle: HINTERNET;
26
Buffer: array[0..1024] of Char;
27
BytesRead: dWord;
28
begin
29
Result := '';
30
NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
31
if Assigned(NetHandle) then
32
begin
33
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
34
if Assigned(UrlHandle) then
35
{ UrlHandle valid? Proceed with download }
36
begin
37
FillChar(Buffer, SizeOf(Buffer), 0);
38
repeat
39
Result := Result + Buffer;
40
FillChar(Buffer, SizeOf(Buffer), 0);
41
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
42
until BytesRead = 0;
43
InternetCloseHandle(UrlHandle);
44
end
45
else
46
{ UrlHandle is not valid. Raise an exception. }
47
raise Exception.CreateFmt('Cannot open URL %s', [Url]);
48
InternetCloseHandle(NetHandle);
49
end
50
else
51
{ NetHandle is not valid. Raise an exception }
52
raise Exception.Create('Unable to initialize Wininet');
53
end;
54
function DownloadToFile(const Url, LFile: string): Boolean;
55
var
56
NetHandle: HINTERNET;
57
UrlHandle: HINTERNET;
58
Buffer: array[0..1024] of Char;
59
BytesRead: dWord;
60
LFileStream: TFileStream;
61
begin
62
Result := False;
63
NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
64
if Assigned(NetHandle) then
65
begin
66
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
67
if Assigned(UrlHandle) then
68
{ UrlHandle valid? Proceed with download }
69
begin
70
LFileStream := TFileStream.Create(LFile, fmCreate);
71
FillChar(Buffer, SizeOf(Buffer), 0);
72
repeat
73
FillChar(Buffer, SizeOf(Buffer), 0);
74
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
75
LFileStream.Write(Buffer, BytesRead);
76
until BytesRead = 0;
77
LFileStream.Free;
78
InternetCloseHandle(UrlHandle);
79
end
80
else
81
{ UrlHandle is not valid. Raise an exception. }
82
raise Exception.CreateFmt('Cannot open URL %s', [Url]);
83
InternetCloseHandle(NetHandle);
84
end
85
else
86
{ NetHandle is not valid. Raise an exception }
87
raise Exception.Create('Unable to initialize Wininet');
88
end;
89
procedure TForm1.Button1Click(Sender: TObject);
90
begin
91
Memo1.Lines.Text := DownloadToString('http://down.360safe.com/')
92
end;
93
procedure TForm1.Button2Click(Sender: TObject);
94
begin
95
DownloadToFile('http://down.360safe.com/inst.exe', 'mailsextractor.exe');//此地址可改,只是測試用的,改成你的實際地址
96
end;
97
end.
例子簡單,僅供參考,高手莫笑哦~