本文介绍了ASP.NET MVC:文件的响应流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从一个MVC操作方法,其中出现以下情况返回FilePathResult(假设,如果它的事项,该文件到的结果指向是非常大的):

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. 该文件被加载在其全部插入服务器的存储器中,然后发送到客户端。

  2. 该文件被以某种方式传输到客户端,在这样一种方式,它是不完全加载到服务器的存储器中的任何点。

  3. 别的东西。

如果答案是1,是有可能有发送作为2的文件由不是返回不同类型的结果

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

推荐答案

更​​新: FilePathResult 使用的其中直接写入指定的文件到一个HTTP响应输出流,无​​需在内存中缓存它。 。

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.

您可以使用 FileStreamResult 类流数据传回:

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

这篇关于ASP.NET MVC:文件的响应流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:17
查看更多