分享

Delphi UrlDownloadToFile 实现文件下载

 远在南非 2010-08-18
Delphi实现文件下载UrlDownloadToFile
UrlDownloadToFile是一个非常不错的函数,我们可以用它来下载各种格式的图片、音乐、文件等等。
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.Button1Click(Sender: TObject);
const
// URL Location
SourceFile:='http://www.0523www.como/images/logo.gif';
// Where to save the file
DestFile := 'c:\3k2.gif';
begin
if DownloadFile(SourceFile, DestFile) then
begin
ShowMessage('下载成功!');
// Show downloaded image in your browser
ShellExecute(Application.Handle,PChar('open'),PChar(DestFile),PChar(''),nil,SW_NORMAL)
end
else
ShowMessage(SourceFile+'下载出错!')
end;

如果涉及批量下载,那么在上例中Button1Click我们可以这样写:
procedure TForm1.Button1Click(Sender: TObject);
//---自己定义一个URL List和文件命名的List---//
begin
if DownloadFile(URLList, FileName) then
begin
ShowMessage('下载成功!');
ShellExecute(Application.Handle,PChar('open'),PChar('路径'+FileName+'.exe'),PChar(''),nil,SW_NORMAL) //这句用不上,就闪掉,只做下载测试用。
end
else
ShowMessage(URLList+'下载出错!')
end;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多