windows 在 Delphi 中下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3506251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading a file in Delphi
提问by Daisetsu
A google search shows a few examples on how to download a file in Delphi but most are buggy and half of the time don't work in my experience.
谷歌搜索显示了一些关于如何在 Delphi 中下载文件的示例,但大多数都有问题,而且有一半的时间在我的经验中不起作用。
I'm looking for a simple robust solution which will let me download a single exe (for updating my app) and will hold the execution of the current update thread until the download is done or errors out. The process is already threaded so the download code should hold execution until it's done (hopefully).
我正在寻找一个简单而强大的解决方案,它可以让我下载单个 exe(用于更新我的应用程序)并保持当前更新线程的执行,直到下载完成或出现错误。该进程已经线程化,因此下载代码应该保持执行直到它完成(希望如此)。
Here's two implementations, both seem very complicated
1. http://www.scalabium.com/faq/dct0116.htm
2. http://delphi.about.com/od/internetintranet/a/get_file_net.htm
这里有两个实现,看起来都很复杂
1. http://www.scalabium.com/faq/dct0116.htm
2. http://delphi.about.com/od/internetintranet/a/get_file_net.htm
回答by Andreas Rejbrand
The second approach is the standard way of using Internet resources using WinINet, a part of Windows API. I have used it a lot, and it has always worked well. The first approach I have never tried. (Neither is "very complicated". There will always be a few additional steps when using the Windows API.)
第二种方法是使用 WinINet(Windows API 的一部分)使用 Internet 资源的标准方法。我已经用了很多次了,而且效果一直很好。第一种方法我从未尝试过。(两者都不是“非常复杂”。使用 Windows API 时总会有一些额外的步骤。)
If you want a very simple method, you could simply call UrlMon.URLDownloadToFile
. You will not get any fine control (at all!) about the download, but it is very simple.
如果你想要一个非常简单的方法,你可以简单地调用UrlMon.URLDownloadToFile
. 您不会对下载进行任何精细的控制(根本!),但它非常简单。
Example:
例子:
URLDownloadToFile(nil,
'http://www.rejbrand.se',
PChar(ExtractFilePath(Application.ExeName) + 'download.htm'),
0,
nil);
回答by Mike Taylor
Why not make use of indy. If you use the TIdHTTP its simple
为什么不使用indy。如果你使用 TIdHTTP 它很简单
procedure DownloadFile;
var
IdHTTP1: TIdHTTP;
Stream: TMemoryStream;
Url, FileName: String;
begin
Url := 'http://www.rejbrand.se';
Filename := 'download.htm';
IdHTTP1 := TIdHTTP.Create(Self);
Stream := TMemoryStream.Create;
try
IdHTTP1.Get(Url, Stream);
Stream.SaveToFile(FileName);
finally
Stream.Free;
IdHTTP1.Free;
end;
end;
You can even add a progress bar by using the OnWork and OnWorkBegin Events
您甚至可以使用 OnWork 和 OnWorkBegin 事件添加进度条
procedure IdHTTPWorkBegin(ASender: TObject; AWorkMode: TWorkMode;AWorkCountMax: Int64);
begin
ProgressBar.Max := AWorkCountMax;
ProgressBar.Position := 0;
end;
procedure IdHTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
ProgressBar.Position := AWorkCount;
end;
I'm not sure if these event fire in the context of the main thread, so any updates done to VCL components may have to be done using the tidnotify component to aviod threading issues. Maybe someone else can check that.
我不确定这些事件是否在主线程的上下文中触发,因此对 VCL 组件所做的任何更新可能必须使用 tidnotify 组件来完成,以避免线程问题。也许其他人可以检查一下。
回答by John Boe
Using URLMon.
使用 URLMon。
errcode := URLMon.URLDownloadToFile(nil,
PChar('http://www.vbforums.com/showthread.php?345726-DELPHI-Download-Files'),
PChar( 'a:\download.htm'),
0,
nil);
if errcode > 0 then
showmessage('Error while downloading: ' + inttostr(errcode));