下载文件并自动保存到文件夹
我正在尝试制作一个用于从我的网站下载文件的用户界面。该站点有 zip 文件,需要将这些文件下载到用户输入的目录中。但是,我无法成功下载该文件,它只是从临时文件夹中打开。
代码:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
string filepath = null;
filepath = textBox1.Text;
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(e.Url, filepath);
}
I'm trying to make a UI for downloading files from my site. The site have zip-files and these need to be downloaded to the directory entered by the user. However, I can't succeed to download the file, it just opens up from a temporary folder.
Code:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
string filepath = null;
filepath = textBox1.Text;
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(e.Url, filepath);
}
Full sourcecode:
http://en.paidpaste.com/LqEmiQ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的程序完全按照您的要求执行,没有提示或任何内容,请参阅以下代码。
这段代码将创建所有必需的目录(如果它们尚不存在):
这段代码将把给定的文件下载到给定的目录(在前面的代码片段创建它之后:
因此,使用这两段代码,您可以创建所有目录,然后告诉下载程序(这不会提示您将文件下载到该位置。
My program does exactly what you are after, no prompts or anything, please see the following code.
This code will create all of the necessary directories if they don't already exist:
This code will download the given file to the given directory (after it has been created by the previous snippet:
So using these two pieces of code you can create all of the directories and then tell the downloader (that doesn't prompt you to download the file to that location.
如果您不想使用“WebClient”或/和需要使用 System.Windows.Forms.WebBrowser,例如因为您想首先模拟登录,则可以使用此扩展的 WebBrowser,它从 Windows 中挂钩“URLDownloadToFile”方法URLMON Lib 并使用 Web 浏览器信息的上下文
: http://www.pinvoke.net/default.aspx/urlmon/URLDownloadToFile%20.html
If you don't want to use "WebClient" or/and need to use the System.Windows.Forms.WebBrowser e.g. because you want simulate a login first, you can use this extended WebBrowser which hooks the "URLDownloadToFile" Method from the Windows URLMON Lib and uses the Context of the WebBrowser
Infos: http://www.pinvoke.net/default.aspx/urlmon/URLDownloadToFile%20.html
为什么不完全绕过 WebClient 的文件处理部分呢?也许与此类似:
Why not just bypass the WebClient's file handling pieces altogether. Perhaps something similar to this:
好吧,你的解决方案几乎有效。为了保持简单,需要考虑一些事项:
仅取消您知道将进行下载的特定 URL 的默认导航,否则用户将无法导航到任何地方。这意味着您不得更改网站下载 URL。
DownloadFileAsync
不知道服务器在Content-Disposition
标头中报告的名称,因此您必须指定一个名称,或者根据原始 URL 计算一个名称(如果是)可能的。您不能只指定文件夹并期望自动检索文件名。您必须处理来自
DownloadCompleted
回调的下载服务器错误,因为 Web 浏览器控件将不再为您执行此操作。示例代码,将下载到
textBox1
中指定的目录,但具有随机文件名,并且没有任何额外的错误处理:此解决方案应该可以工作,但很容易被破坏。尝试考虑一些列出可供下载的可用文件的 Web 服务,并为其制作自定义 UI。它会更简单,您将控制整个过程。
Well, your solution almost works. There are a few things to take into account to keep it simple:
Cancel the default navigation only for specific URLs you know a download will occur, or the user won't be able to navigate anywhere. This means you musn't change your website download URLs.
DownloadFileAsync
doesn't know the name reported by the server in theContent-Disposition
header so you have to specify one, or compute one from the original URL if that's possible. You cannot just specify the folder and expect the file name to be retrieved automatically.You have to handle download server errors from the
DownloadCompleted
callback because the web browser control won't do it for you anymore.Sample piece of code, that will download into the directory specified in
textBox1
, but with a random file name, and without any additional error handling:This solution should work but can be broken very easily. Try to consider some web service listing the available files for download and make a custom UI for it. It'll be simpler and you will control the whole process.
看看 http://www.csharp-examples.net/download-files/
以及 webclient 上的 msdn 文档
http://msdn.microsoft.com/en-us/library /system.net.webclient.aspx
我的建议是尝试同步下载,因为它更简单。
在尝试此操作时,您可能会了解 Web 客户端参数是否错误或文件格式不正确。
这是一个代码示例..
Take a look at http://www.csharp-examples.net/download-files/
and msdn docs on webclient
http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
My suggestion is try the synchronous download as its more straightforward.
you might get ideas on whether webclient parameters are wrong or the file is in incorrect format while trying this.
Here is a code sample..
一个更简单的解决方案是使用 Chrome 下载文件。通过这种方式,您不必手动单击“保存”按钮。
A much simpler solution would be to download the file using Chrome. In this manner you don't have to manually click on the save button.