在 Delphi 中下载文件 [英] Downloading a file in Delphi

查看:31
本文介绍了在 Delphi 中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌搜索显示了一些关于如何在 Delphi 中下载文件的示例,但大多数都存在问题,而且有一半的时间在我的经验中不起作用.

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.

我正在寻找一个简单而强大的解决方案,它可以让我下载单个 exe(用于更新我的应用程序)并保持当前更新线程的执行,直到下载完成或出现错误.该进程已经线程化,因此下载代码应该保持执行直到它完成(希望如此).

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).

这里有两个实现,看起来都很复杂
1. http://www.scalabium.com/faq/dct0116.htm
2. http://delphi.about.com/od/internetintranet/a/get_file_net.htm

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

推荐答案

第二种方法是使用 WinINet(Windows API 的一部分)使用 Internet 资源的标准方法.我已经用了很多次了,而且效果一直很好.第一种方法我从未尝试过.(两者都不是非常复杂".使用 Windows API 时总会有一些额外的步骤.)

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.)

如果你想要一个非常简单的方法,你可以简单地调用 UrlMon.URLDownloadToFile.您不会对下载进行任何精细的控制(根本!),但它非常简单.

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.

示例:

URLDownloadToFile(nil,
                  'http://www.rejbrand.se',
                  PChar(ExtractFilePath(Application.ExeName) + 'download.htm'),
                  0,
                  nil);

这篇关于在 Delphi 中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆