打开PDF结果与MVC 3浏览器选项卡 [英] Open PDF result in browser tab with MVC 3
问题描述
我使用ASP.NET MVC 3,我有一个返回PDF文件这样的控制器动作:
I am using ASP.NET MVC 3. I have a controller action that returns a PDF file like this:
Public Class ReportController
...
Function Generate(id As Integer) As ActionResult
...
Return File(output, "application/pdf", "something.pdf")
' "output" is a memory stream
End Function
在code的作品,但Firefox不会在标签显示的结果,结果被下载或使用Adobe Reader打开。
The code works but Firefox doesn't display the result in a tab, the result is either downloaded or opened with Adobe Reader.
我知道的火狐可以在标签显示PDF 的,因为我可以只是谷歌的一些PDF,点击链接,和PDF将在标签中打开。
I know that Firefox can display PDF in a tab because I can just google some PDF, click the link, and the PDF will open in a tab.
我如何设置的动作使PDF将在标签中打开?
How do I set up the action so the PDF will open in a tab?
推荐答案
我从<一个答案href=\"http://stackoverflow.com/questions/3724278/asp-net-mvc-how-can-i-get-the-browser-to-open-and-display-a-pdf-instead-of-displ/3724471#3724471\">related右侧链接:
Response.AppendHeader("Content-Disposition", "inline")
Return File(output, "application/pdf")
该PDF在选项卡中打开,但文件名提示丢失,即使我做这样的:
The PDF opens in a tab, but the filename hint is lost, even if I do it like this:
Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
Return File(output, "application/pdf", "something.pdf")
所以最后我没有刻意去给文件名暗示都没有。
So finally I didn't bother to give filename hint at all.
修改
ASP.NET MVC 3的文件有3个参数:
ASP.NET MVC 3's File with 3 parameters:
Return File(output, "application/pdf", "something.pdf")
将新增内容处置:附件;文件名=something.pdf
来响应头,即使已经有一个内容处置的响应头。
will add Content-Disposition: attachment; filename="something.pdf"
to the response header, even if there is already a Content-Disposition in the response header.
所以,如果你手动添加内容处置的标题,然后使用文件与3个参数,你最终的两个内容处置头。火狐8会说,如果响应头是这样的反应被破坏。
So if you manually added Content-Disposition to the header, and then use File with 3 parameters, you end up with two Content-Disposition headers. Firefox 8 will say that the response is corrupted if the response header is like this.
所以,最好的办法现在要做的是添加内容处置手动为内联,然后用文件有两个参数:
So best way to do it now is add Content-Disposition manually for 'inline', and then use File with 2 parameters:
Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
Return File(output, "application/pdf")
这篇关于打开PDF结果与MVC 3浏览器选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!