我如何下载一个大文件(通过HTTP)的.NET? [英] How do I download a large file (via HTTP) in .NET?

查看:106
本文介绍了我如何下载一个大文件(通过HTTP)的.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载的的文件(2  GB)通过HTTP在C#控制台应用程序。问题是,经过约1.2  GB,应用程序运行的内存

I need to download a large file (2 GB) over HTTP in a C# console application. Problem is, after about 1.2 GB, the application runs out of memory.

这里的code我使用:

Here's the code I'm using:

WebClient request = new WebClient();
request.Credentials = new NetworkCredential(username, password);
byte[] fileData = request.DownloadData(baseURL + fName);

正如你所看到的......我直接把文件读入内存中。我是pretty的肯定,我可以解决这个问题,如果我要读取的数据从HTTP回块,并将其写入到磁盘上的文件。

As you can see... I'm reading the file directly into memory. I'm pretty sure I could solve this if I were to read the data back from HTTP in chunks and write it to a file on disk.

我怎么能这样做呢?

推荐答案

如果您使用<一个href="http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfile%28VS.80%29.aspx">WebClient.DownloadFile你可以直接将其保存到一个文件中。

If you use WebClient.DownloadFile you could save it directly into a file.

这篇关于我如何下载一个大文件(通过HTTP)的.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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