我有一個大的zip文件(500MB或更大),我正在讀入一個MemoryStream並作爲FileStreamResult返回。但是,對於超過200MB的文件,我得到了OutOfMemory異常。在我的行動,我有以下代碼:ASP MVC FileStreamResult OutOfMemoryException
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);
我不想使用ReadAllBytes,因爲2GB的限制,也是因爲一次將整個文件讀入內存時的內存問題。 – user327999 2012-04-04 14:25:06