Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 654
Tim Ferrell Avatar asked Oct 03 '12 13:10

Tim Ferrell


2 Answers

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

Response.BufferOutput = false;
return File(fileStream, contentType);
like image 98
phantom Avatar answered Sep 18 '22 12:09

phantom


Yes you can by using Web Api in order to stream the file, take a look at this article Dealing with large files in ASP.NET Web API

like image 40
Stefan P. Avatar answered Sep 20 '22 12:09

Stefan P.