FilePathResult throws an OutOfMemoryException with a large file

I have this action in my controller that 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 works fine, but the problem is that when the file is large (just over 220 MB), it throws OutOfMemoryException.

This is a 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?

+4
source share
2 answers

This is because your request uses PageInspector, as indicated in this stack trace line:

Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write (Byte buffer [], Int32 offset, Int32 count) +106

To fix this, uncheck the browser in box Visual Studioas shown in the following screenshot:

Uncheck browser link

+9

, , maxQueryString Web.config. , :

 <system.webServer>
   <security>
      <requestFiltering>
        <requestLimits maxUrl="10999" maxQueryString="9999" />
      </requestFiltering>
    </security>
 </system.webServer>

.

0

All Articles