ASP.NET MVC:如何让浏览器打开并显示 PDF 而不是显示下载提示? [英] ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?

查看:29
本文介绍了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(...

这篇关于ASP.NET MVC:如何让浏览器打开并显示 PDF 而不是显示下载提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆