MVC - FileContentResult发送损坏的pdf

时间:2016-02-23 22:05:35

标签: asp.net-mvc asp.net-mvc-5 iis-8

我有一个简单的操作方法,它返回一个PDF文档,该文档显示在带有<iframe>标记的<embed>中,每次调用此方法都会返回一个损坏的PDF。 (我通过使用开发工具来保存服务器的响应,确定其已损坏)

行动方法:

public FileContentResult GetPdfReport(string Id)
{
    Response.AppendHeader("Content-Disposition", "inline; filename=report.pdf");
    var content = System.IO.File.ReadAllBytes(Server.MapPath("~/Reports/Testfile.pdf"));
    System.IO.File.WriteAllBytes(Server.MapPath("~/Reports/debugReport.pdf"), content);
    return File(content, "application/pdf");
}

查看内容:

<embed id="widgetResponsePdf" src="@Url.Action("GetPdfReport", "WidgetResponse", new { Id = "123" })" type="application/pdf" onmouseout="mouseOutHandler();" />

当我收到损坏的PDF文件时,文件TestFile.pdf和debugReport.pdf就打开了,正常请求/响应和损坏的请求/响应之间的请求和响应头没有区别。

IIS中是否存在一些我错过的设置,可能导​​致请求之间的行为不一致,或者这可能仅由网络问题引起?

1 个答案:

答案 0 :(得分:1)

在我们的示例中,IFrame有一个 src 属性,该属性指向加载<embed>内容的部分视图,该内容具有 src 属性,指向实际的PDF文件,而不是返回PartialView的{​​{1}}。以下示例已从我们的实际实施中简化 部分视图

FileContentResult

<强>控制器

<iframe> <!-- iframe is actually loaded from another partial view, it is show here to simplify things -->
    <embed 
         src='@Url.Content(Model.ReportFileName)' 
         type="application/pdf">
    </embed>
</iframe>

由于IE支持的站点限制,我们的用户(Intranet站点)需要安装AdobeAcrobat才能查看PDF。虽然此解决方案适用于我们,但对于面向互联网的网站而言,这可能并不理想。

相关问题