Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

c# - 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);
}

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.

This is the Stack Trace

[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

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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[] buffer, Int32 offset, Int32 count) +106

To fix this, uncheck the browser checkbox in Visual Studio as in the following screen shot:

Uncheck Browser Link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...