我如何下载一个大文件(通过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.

我怎么能这样做呢?

如果您使用WebClient.DownloadFile你可以直接将其保存到一个文件中。

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