[ACCEPTED]-Better way to download a binary file-download
Accepted answer
Instead of
context.Response.BinaryWrite(bytes);
use
context.Response.TransmitFile(context.Server.MapPath(url));
This will avoid reading the 1 entire file into memory.
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);
toFile.Flush();
}
}
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.