C# ActionResult 返回一个流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13983190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:15:29  来源:igfitidea点击:

ActionResult returning a Stream

c#asp.net-mvc

提问by Nate Pet

My ActionResultreturns a Filebut I also need it to conditionally return a Stream.

ActionResult返回 aFile但我也需要它有条件地返回 a Stream

I have not been able to find documentation on how an ActionResultcan return a Stream.

我无法找到有关如何ActionResult返回Stream.

Here is my code for return of a file:

这是我返回文件的代码:

    return File(memoryStream,.... )

As mentioned, I need to return just a Stream.

如前所述,我只需要返回一个Stream.

采纳答案by aifarfa

Updated for MVC5 2020:

为 MVC5 2020 更新:

my previous answer was dated.

我之前的回答已经过时了。

as of now, the Filereturns different type of ActionResult depends on given arguments

截至目前,File返回不同类型的 ActionResult 取决于给定的参数

// to return FileStreamResult
return File(memoryStream, "application/pdf");
// or..
return File(memoryStream, "application/pdf", "file_name");


Use FileStreamResult:

使用FileStreamResult

MemoryStream stream = someService.GetStream();

return new FileStreamResult(stream, "application/pdf")

回答by Darin Dimitrov

Having an action call another action is a design smell. You should avoid it. Simply put the logic that needs to be reused between the 2 actions in a service layer. And then call this logic from your 2 actions.

让一个动作调用另一个动作是一种设计味道。你应该避免它。简单的把需要重用的逻辑放在一个服务层的2个动作之间。然后从您的 2 个操作中调用此逻辑。

For example:

例如:

public ActionResult Action1()
{
    Stream stream = service.GetStream();
    // ... do something with the stream and return a view for example
    return View();
}

public ActionResult Action2()
{
    Stream stream = service.GetStream();
    // let's return the stream to the client so that he could download it as file
    return File(stream, "application/pdf");
}

Now you no longer need to call the second action from the first one.

现在您不再需要从第一个操作中调用第二个操作。

回答by Mark Amery

The shortest way to use a Streamas the result of an Action Method in a Controlleris the one you already showed in the question: use the Filehelper method of Controller. This returns a FileStreamResult.

将 aStream用作a中操作方法的结果的最短方法Controller是您在问题中已经展示的File方法:使用 的辅助方法Controller。这将返回一个FileStreamResult.

There are a couple of overloads available that take a Stream. Both overloads require the MIME type of the response to be specified, which will be emitted as the Content-Typeheader of the response; if your circumstances are such that this is unknown to your application, you could always specify text/plainor application/octet-streamfor arbitrary text or binary data, respectively. One overload additionally takes a third parameter that sets the filename to show in the browser's download dialogue (controlled via the Content-Dispositionheader), if applicable.

有几个可用的重载需要一个Stream. 两种重载都需要指定响应的 MIME 类型,该类型将作为Content-Type响应的标头发出;如果您的情况是您的应用程序未知,您可以始终分别指定text/plainapplication/octet-stream任意文本或二进制数据。Content-Disposition如果适用,一个重载还采用第三个参数,用于设置文件名以显示在浏览器的下载对话框中(通过标题控制)。

Overload signatures:

重载签名

protected internal FileStreamResult File(
    Stream fileStream,
    string contentType
)

and

protected internal virtual FileStreamResult File(
    Stream fileStream,
    string contentType,
    string fileDownloadName
)

Example usage:

用法示例

return File(myStream, "application/pdf");

or

或者

return File(myStream, "application/pdf", "billing-summary.pdf");