ArgumentNullException. Существует никакой требование для вызова дополнительных методов, как будто они были методами экземпляра. Можно назвать их, как будто они были нормальными методами. NullReferenceException был бы абсолютно неправильным в этом случае.
Instead of
context.Response.BinaryWrite(bytes);
use
context.Response.TransmitFile(context.Server.MapPath(url));
This will avoid reading the 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 is that you use a smaller buffer and flush the write stream to clear out memory.
Right now you are holding the entire file that you are downloading in both your BinaryReader and BinaryWriter. Chunking the download into a smaller buffer alleviates this burden on memory.