将生成的PDF数据加载到ASP.NET MVC上的IFRAME中 [英] Load Generated PDF Data into IFRAME on ASP.NET MVC

查看:99
本文介绍了将生成的PDF数据加载到ASP.NET MVC上的IFRAME中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ASP.NET website that generates a report as a PDF. I want to load that PDF into and IFRAME on my View. But when I try, it always prompts me to download the PDF. I have tried using the File() method, returning a new FileContectResult(), and even just using Response.BinaryWrite() but nothing seems to do the trick. Anyone have any good advice?

这是视图中的Razor代码:

Here is the Razor code from the View:

<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>

这是我的控制器中合适的方法:

And here is the appropriate method from my controller:

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

我知道我缺少一些简单而明显的东西,但是在卑鄙的一生中,我可以确定它是什么.

I know I am missing something simple and obvious, but for the life of mean I can determine what it is.

推荐答案

如果可能的话,我将放弃iframe和javascript并使用<embed>

If possible, I would ditch the iframe and javascript and go for <embed>

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

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

这篇关于将生成的PDF数据加载到ASP.NET MVC上的IFRAME中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆