ASP.NET MVC: file response streaming?

牧云@^-^@ 提交于 2019-12-23 23:26:17

问题


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?


回答1:


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");


来源:https://stackoverflow.com/questions/16845681/asp-net-mvc-file-response-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!