ASP.NET MVC:如何让浏览器打开并显示 PDF 而不是显示下载提示?

ASP.NET MVC:如何让浏览器打开并显示 PDF 而不是显示下载提示?

问题描述:

好的,所以我有一个生成 PDF 并将其返回给浏览器的操作方法.问题是 IE 不会自动打开 PDF,而是显示下载提示,即使它知道它是什么类型的文件.Chrome 也做同样的事情.在这两种浏览器中,如果我单击指向存储在服务器上的 PDF 文件的链接,它会打开得很好并且永远不会显示下载提示.

Ok, so I have an action method that generates a PDF and returns it to the browser. The problem is that instead of automatically opening the PDF, IE displays a download prompt even though it knows what kind of file it is. Chrome does the same thing. In both browsers if I click a link to a PDF file that is stored on a server it will open up just fine and never display a download prompt.

这里是调用返回PDF的代码:

Here is the code that is called to return the PDF:

public FileResult Report(int id)
{
    var customer = customersRepository.GetCustomer(id);
    if (customer != null)
    {
        return File(RenderPDF(this.ControllerContext, "~/Views/Forms/Report.aspx", customer), "application/pdf", "Report - Customer # " + id.ToString() + ".pdf");
    }
    return null;
}

这是来自服务器的响应头:

Here's the response header from the server:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 16 Sep 2010 06:14:13 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Content-Disposition: attachment; filename="Report - Customer # 60.pdf"
Cache-Control: private, s-maxage=0
Content-Type: application/pdf
Content-Length: 79244
Connection: Close

我是否必须在响应中添加一些特殊的东西才能让浏览器自动打开 PDF?

Do I have to add something special to the response to get the browser to open the PDF automatically?

非常感谢任何帮助!谢谢!

Any help is greatly appreciated! Thanks!

Response.AppendHeader("Content-Disposition", "inline; filename=foo.pdf");
return File(...