[ACCEPTED]-Better way to download a binary file-download

Accepted answer
Score: 17

Instead of




This will avoid reading the 1 entire file into memory.

Score: 3

Try something like this:

using (var br = new BinaryReader(fs))
    FileStream toFile = File.OpenWrite(ToFileName);
    byte[] buff = new byte[2000];
    while (reader.Read(buff, 0, 2000) > 0)
        toFile.Write(buff, 0, 2000);

The important thing 7 is that you use a smaller buffer and flush 6 the write stream to clear out the memory.

Right 5 now you are holding the entire file that 4 you are downloading in both your BinaryReader 3 and BinaryWriter. Chunking the download 2 into a smaller buffer alleviates this burden 1 on memory.

More Related questions