FilePathResult抛出带有大文件的OutOfMemoryException [英] FilePathResult thrown an OutOfMemoryException with large file
问题描述
我的控制器中有此操作,该操作会将文件返回给用户.
I have this Action in my controller which returns a file to the user.
public virtual ActionResult ReturnFile(string fileName, string filePath, string contentType)
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
// set token for close the modal-window
CookiesHelper.SetValueDownloadTokenInCookie();
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filePath, contentType);
}
它工作正常,但是问题在于,当文件很大(略大于220 Mb)时,它会抛出OutOfMemoryException
.
It is working fine, but the problem is that when the file is large (a little more than 220 Mb) it is throwing an OutOfMemoryException
.
这是堆栈跟踪
[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
System.IO.MemoryStream.set_Capacity(Int32 value) +93
System.IO.MemoryStream.EnsureCapacity(Int32 value) +64
System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +330
Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte[] buffer, Int32 offset, Int32 count) +106
System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +9509748
System.Web.HttpResponse.FilterOutput() +104
System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +49
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
有什么想法吗?
推荐答案
这是因为您的请求正在使用PageInspector
,如堆栈跟踪的这一行所示:
It is because your request is using PageInspector
as indicated by this line of your stack trace:
Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte []缓冲区,Int32偏移量,Int32计数)+106
Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte[] buffer, Int32 offset, Int32 count) +106
要解决此问题,请取消选中Visual Studio
中的浏览器复选框,如以下屏幕截图所示:
To fix this, uncheck the browser checkbox in Visual Studio
as in the following screen shot:
这篇关于FilePathResult抛出带有大文件的OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!