我有一个大 zip 文件(500MB 或更大),我正在读入 MemoryStream 并作为 FileStreamResult 返回。但是,对于超过 200MB 的文件,我收到了 OutOfMemory 异常。在我的操作中,我有以下代码:
MemoryStream outputStream = new MemoryStream();
using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
//Response.BufferOutput = false; // to prevent buffering
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
}
}
outputStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(outputStream, content_type);