How do I download a large file (via HTTP) in .NET?

Nick Cartwright picture Nick Cartwright · Jul 3, 2009 · Viewed 39.2k times · Source

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.

Here's the code I'm using:

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

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.

How could I do this?

Answer

Alex Peck picture Alex Peck · Jul 3, 2009

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