2

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?

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.

Peter Lange
  • 2,786
  • 2
  • 26
  • 40
  • Check this - http://stackoverflow.com/questions/291813/recommended-way-to-embed-pdf-in-html – malkam Apr 03 '15 at 06:55

2 Answers2

4

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")" />
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
0

Remember to add

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

Although I had same issue,but none of the solutions not worked in Firefox until I changed the Options of my browser. In Options

window,then Application Tab change the Portable Document Format to Preview in Firefox.

sosha
  • 207
  • 3
  • 11