ASP.NET MVC: Returning large amounts of data from FileResult

asp.net, asp.net-mvc, controller, out-of-memory

Solution

You can disable the response buffer before you return the file result.

Response.BufferOutput = false;
return File(fileStream, contentType);

Problem

I have a file browser application in MVC4 that allows you to download a selected file from a controller. Currently, the FileResult returns the Stream of the file, along with the other response headers. While this works fine for smaller files, files that are larger generate an OutOfMemoryException. What I'd like to do is transmit the file from the controller, without buffering in memory in a fashion similiar to HttpReponse.TransmitFile in WebForms. How can this be accomplished?

Original source