[英]Load Generated PDF Data into IFRAME on ASP.NET MVC
我有一個ASP.NET網站,以PDF格式生成報告。 我想在我的View上加載PDF和IFRAME。 但是當我嘗試時,它總是提示我下載PDF。 我已經嘗試使用File()方法,返回一個新的FileContectResult(),甚至只使用Response.BinaryWrite(),但似乎沒有什么可以做的。 有人有什么好建議嗎?
以下是View中的Razor代碼:
<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");
}
我知道我錯過了一些簡單而明顯的東西,但對於平均生活,我可以確定它是什么。
如果可能的話,我會拋棄iframe和javascript並轉到<embed>
public ActionResult ContactListPDF2()
{
byte[] reportData = ReportsModel.GetContactListPDF();
return new FileContentResult(reportData, "application/pdf");
}
<embed src="@Url.Action("ContactListPDF2","Reports")" />
記得加
Response.AppendHeader("Content-Disposition", "inline; filename=name.pdf");
雖然我遇到了同樣的問題,但是在我改變瀏覽器的選項之前,沒有一個解決方案在Firefox中不起作用。 在Options
窗口,然后Application Tab
將Portable Document Format
更改為Preview in Firefox
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.