2016-02-23 74 views
0

我有一个简单的操作方法,它返回一个PDF文档,该文档在中以<embed>标记显示,每调用​​一次该方法都会返回一个损坏的PDF。 (我已经决定了它的使用开发工具来保存来自服务器的响应损坏)MVC - FileContentResult发送损坏的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();" /> 

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

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

+0

我不确定,但尝试设置'content.Position = 0;'返回字符串之前 –

+0

谢谢,但内容是一个字节数组,而不是流 –

+0

您确定PDF是否正确?另外,尝试将FileContentResult更改为ActionResult –

回答

1

在我们的情况下,该IFrame有SRC属性指向该加载<embed>内容,然后有一个SRC属性指向实际的PDF文件,而不是一个PartialView返回一个局部视图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> 

控制器

public PartialView GetPdfReport(string Id) 
{ 
    var model = PDFResponseModel 
    { 
     ReportFileName = Server.MapPath("~/Reports/Testfile.pdf") 
    }; 
    return PartialView(model); 
} 

由于网站限制对IE的支持简化,我们的用户(Intranet网站)都需要安装AdobeAcrobat查看PDF文件。尽管此解决方案适用于我们,但面向互联网的网站可能并不理想。