Load Generated PDF Data into IFRAME on ASP.NET MVC

10,576

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")" />
Share:
10,576
Peter Lange
Author by

Peter Lange

Despite my boyish good looks, I really am quite intelligent.

Updated on June 04, 2022

Comments

  • Peter Lange
    Peter Lange almost 2 years

    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
    Peter Lange about 9 years
    I hadn't thought about the embed tag. Is it mobile friendly?