2015-04-03 110 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

相關問題