2015-04-03 107 views
0

我有一个ASP.NET网站,它以PDF形式生成报告。我想将该PDF加载到我的视图中的IFRAME中。但是当我尝试时,它总是会提示我下载PDF。我尝试过使用File()方法,返回一个新的FileContectResult(),甚至只是使用Response.BinaryWrite(),但似乎没有什么窍门。任何人有任何好的建议?将生成的PDF数据加载到ASP.NET MVC的IFRAME中

这里是从视图的剃刀代码:

<script> 
    $(document).ready(function() { 
     var url = $('#reportUrl').val(); 
     $("#iframe1").attr("src", url); 
    }); 
</script> 
<input type="hidden" id="reportUrl" value="@Url.Action("ContactListPDF2","Reports")" /> 
<div class="block-head"> 
    <div class="item-content"> 
     <iframe id="iframe1" style="border: 1px solid black; height: 500px; width: 100%"> 

     </iframe> 
    </div> 
</div> 

这里是我的控制器适当的方法:

public ActionResult ContactListPDF2() 
{ 
    byte[] reportData = ReportsModel.GetContactListPDF(); 
    return new FileContentResult(reportData, "application/pdf"); 
} 

我知道我失去了一些东西简单明了,但对于意味着我的生活可以确定它是什么。

+0

选中此项 - http://stackoverflow.com/questions/291813/recommended-way-to-embed-pdf-in-html – malkam 2015-04-03 06:55:06

回答

3

如果可能的话,我会沟的iframe和JavaScript去<embed>

public ActionResult ContactListPDF2() 
{ 
    byte[] reportData = ReportsModel.GetContactListPDF(); 
    return new FileContentResult(reportData, "application/pdf"); 
} 

<embed src="@Url.Action("ContactListPDF2","Reports")" /> 
+0

我没有想过嵌入标签。移动友好吗? – 2015-04-03 05:42:56

+0

是的,它适用于所有主流浏览器。 – Shoe 2015-04-03 14:18:20

0

记得添加

Response.AppendHeader("Content-Disposition", "inline; filename=name.pdf"); 

虽然我有同样的问题,但没有解决方案的没有奏效Firefox,直到我更改了浏览器的选项。在Options

窗口,然后Application Tab更改Portable Document FormatPreview in Firefox

相关问题