我正在使用 ASP.NET MVC 3。我有一个 Controller 操作,它返回一个 PDF 文件,如下所示:
Public Class ReportController
...
Function Generate(id As Integer) As ActionResult
...
Return File(output, "application/pdf", "something.pdf")
' "output" is a memory stream
End Function
该代码有效,但 Firefox 不会在选项卡中显示结果,结果要么是下载的,要么是用 Adobe Reader 打开的。
我知道 Firefox 可以在选项卡中显示 PDF,因为我可以用谷歌搜索一些 PDF,单击链接,然后 PDF 将在选项卡中打开。
如何设置操作以便 PDF 将在选项卡中打开?
最佳答案
我从 related links on the right 得到了答案:
Response.AppendHeader("Content-Disposition", "inline")
Return File(output, "application/pdf")
PDF 在选项卡中打开,但文件名提示丢失,即使我这样做:
Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
Return File(output, "application/pdf", "something.pdf")
所以最后我根本没有费心提供文件名提示。
编辑
带有 3 个参数的 ASP.NET MVC 3 文件:
Return File(output, "application/pdf", "something.pdf")
将添加
Content-Disposition: attachment; filename="something.pdf"
到响应 header ,即使响应 header 中已经存在 Content-Disposition。因此,如果您手动将 Content-Disposition 添加到标题,然后使用带有 3 个参数的 File,您最终会得到 两个内容处置 header 。如果响应头是这样的,Firefox 8 会说响应已损坏。
所以现在最好的方法是为“内联”手动添加 Content-Disposition,然后使用带有 2 个参数的 File:
Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
Return File(output, "application/pdf")
关于asp.net-mvc-3 - 使用 MVC 3 在浏览器选项卡中打开 PDF 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168846/