Лучший способ загрузить двоичный файл?

ArgumentNullException. Существует никакой требование для вызова дополнительных методов, как будто они были методами экземпляра. Можно назвать их, как будто они были нормальными методами. NullReferenceException был бы абсолютно неправильным в этом случае.

5
задан Peter Mortensen 18 November 2009 в 19:49
поделиться

2 ответа

Instead of

context.Response.BinaryWrite(bytes);

use

context.Response.TransmitFile(context.Server.MapPath(url));

This will avoid reading the entire file into memory.

16
ответ дан 18 December 2019 в 08:29
поделиться

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.

3
ответ дан 18 December 2019 в 08:29
поделиться
Другие вопросы по тегам:

Похожие вопросы: