简体   繁体   English

如何在WinForm应用程序中从Web下载文件

[英]How to download a file from the web within a WinForm application

I'm writting a C# utility program that I need the ability to download and save a file from a URL. 我正在编写一个C#实用程序,我需要能够从URL下载并保存文件。

I have the code working to obtain the URL from a web service call, however I'm having trouble finding a simple example of how to begin the download and save the data to a disk file. 我有代码工作从Web服务调用获取URL,但是我找不到如何开始下载并将数据保存到磁盘文件的简单示例。

Does anyone have a good example of this, or can provide me with an example? 有没有人有这方面的好例子,或者可以为我提供一个例子?

Thanks! 谢谢!

EDIT - I should mention that these files will be 3 to 4 GB in size. 编辑 - 我应该提到这些文件的大小为3到4 GB。 So if there are special considerations for files of this size I would appreciate any advice. 因此,如果对这种大小的文件有特殊考虑,我将不胜感激任何建议。

WebClient.DownloadData , the spec contains a small sample. WebClient.DownloadData ,规范包含一个小样本。 It is arguably more efficient to use WebRequest.GetResponseStream and save the data chunk by chunk, but you'll have to properly prepare the WebRequest yourself. 使用WebRequest.GetResponseStream并按块保存数据块可能更有效,但您必须自己准备好WebRequest

Updated 更新

If you have to download 3-4GB files then you must do much much more than what the .Net framework offers. 如果你必须下载3-4GB文件,那么你必须做的远远超过.Net框架提供的内容。 WebClient is immedeatly out-of-the-question since it returns the content as one monolithic byte[]. WebClient是一个毫无疑问的问题,因为它将内容作为一个整体字节[]返回。 Even presuming that your VAS (virtual address space) has the contigous 4GB to burn on these downloads, .Net cannot alocate anything larger than 2Gb (on x64 as well). 即使假设您的VAS(虚拟地址空间)在这些下载中 4GB的连续刻录,.Net也无法添加大于2Gb的任何内容 (在x64上也是如此)。 So you must use streams, as in GetResponseStream(). 所以你必须使用流,就像在GetResponseStream()中一样。

Second you must implement HTTP range units in your request, as per HTTP/1.1 section 3.12 . 其次,您必须在请求中实现HTTP范围单元,具体取决于HTTP / 1.1第3.12节 Your request must contain Content-Range headers to be able to resume intrerupted downloads. 您的请求必须包含Content-Range标头才能恢复不间断的下载。 And of course, your target server will have to accept and recognize these headers, and perhaps respond with a prpoer accept-ranges , which few servers do. 当然,您的目标服务器必须接受并识别这些标头,并且可能使用少数服务器执行的prpoer accept-ranges响应。

You have your plate full, downloading 4Gb is anything but trivial. 你有你的盘子,下载4Gb是微不足道的。

如果你不想在下载过程中冻结UI,只需使用WebClient.DownloadFile (或WebClient.DownloadFileAsync

Since you have gigantic files, prepare for some kind of connection recovery. 由于您有巨大的文件,请准备某种连接恢复。

Whatever method you will find out to get something from the http (WebClient, TcpStream, ...) you should probably code with recovery in mind from the start. 无论你从http(WebClient,TcpStream,......)中找到什么方法,你都应该从头开始编写恢复代码。 That should be focus here. 这应该是重点。

For that, it would be imperative to check if Stream returned from GetResponseStream() supports Seek() . 为此,必须检查从GetResponseStream()返回的Stream是否支持Seek()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM