Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: file response streaming?

When I return a FilePathResult from an MVC action method, which of the following happens (assuming, if it matters, that the file to which the result points is very large):

  1. The file gets loaded in its entirety into the server's memory, and then sent to the client.
  2. The file is somehow streamed to the client, in such a way that it is not at any point fully loaded into the server's memory.
  3. Something else.

If the answer is 1, is it possible to have the file sent as in 2 instead by returning a different type of result?

like image 858
Duncan Avatar asked Mar 03 '26 05:03

Duncan


1 Answers

UPDATE: FilePathResult uses response.TransmitFile which "Writes the specified file directly to an HTTP response output stream, without buffering it in memory.". Here's the source code for MVC.

You can stream data back using the FileStreamResult class:

 return new FileStreamResult(stream, "application/pdf")
 {
     result.FileDownloadName = "somefile.pdf";
 };

Or you could redirect to the file like this:

 return Redirect("somefile.pdf");
like image 59
bendytree Avatar answered Mar 05 '26 20:03

bendytree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!