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.

1

2 Answers 2

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")" />
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.