相关疑难解决方法(0)

在具有名称的浏览器中使用ASP.NET MVC FileContentResult流文件?

有没有办法在浏览器中使用具有特定名称的ASP.NET MVC FileContentResult来流式传输文件?

我注意到你可以有一个FileDialog(打开/保存),或者你可以在浏览器窗口中流式传输文件,但是当你尝试保存文件时它会使用ActionName.

我有以下场景:

byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote);
result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId));
Run Code Online (Sandbox Code Playgroud)

当我使用它时,我可以流式传输字节,但是给用户提供了一个OPEN/SAVE文件对话框.我想在浏览器窗口中实际传输此文件.

如果我只使用FilePathResult,它会在浏览器窗口中显示该文件,但是当我单击"保存"按钮将文件保存为PDF时,它会显示操作名称作为文件的名称.

有没有遇到过这个?

asp.net-mvc filecontentresult fileresult

58
推荐指数
5
解决办法
11万
查看次数

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

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

以下是调用以返回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;
}
Run Code Online (Sandbox Code Playgroud)

这是服务器的响应头:

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
Run Code Online (Sandbox Code Playgroud)

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

任何帮助是极大的赞赏!谢谢!

pdf asp.net-mvc

40
推荐指数
2
解决办法
3万
查看次数

标签 统计

asp.net-mvc ×2

filecontentresult ×1

fileresult ×1

pdf ×1