首页 / 软件开发 / Delphi / 用Delphi实现文件下载的几种方法
用Delphi实现文件下载的几种方法2012-01-15笔者最近开发的系统中需要写一个下载文件的功能。以前用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 := "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;
=================