Home > database >  Unable to embed PDF into the ASP.NET MVC view
Unable to embed PDF into the ASP.NET MVC view

Time:03-17

I have an ASP.NET MVC application that returns a PDF stream. I am trying to embed that stream into a view. I have this action in a controller

[AuthorizeResultVisible]
public ActionResult GetPDFStream(int reportId)
{
        byte[] pdfByteStream = GetReportBytes(reportId);
        Response.Clear();
        Response.AddHeader("Content-Disposition","inline; filename=sample.pdf");
        Response.AddHeader("Content-Type","application/pdf");
        Response.ClearHeaders();
        Response.AddHeader("Content-Length", pdfByteStream.Length.ToString());

        return new FileContentResult(pdfByteStream, "application/pdf");
}

In my view I am trying both, the object tag and/or the iframe tag

<object data="'<%= Url.Action("GetPDFStream", "ResultActions", new {reportId = Model.reportId }) %>" #zoom=150′ width = "100%" height="525" id="iFramePdf" frameBorder="1" type="application/pdf"/>
<iframe src="'<%= Url.Action("GetPDFStream", "ResultActions", new {reportId = Model.reportId }) %>"/>

However, no matter what I do, the embedded PDF doesn't show. Instead, the error message is popping up saying

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

When I get into a source, however, I am able to retrieve the link and confirm that the link works. Once I paste the link into a browser, it does display the PDF, but no PDF is displayed initially

Can someone please help

Thank you in advance

CodePudding user response:

Found a solution by adding Request.Url.GetLeftPart(UriPartial.Authority)

<iframe src="**<%= Request.Url.GetLeftPart(UriPartial.Authority)**   Url.Action("GetPDFStream", "ResultActions", new {reportId = Model.reportId }) %>" #zoom=150′ width = "100%" height="525" id="iFramePdf" frameBorder="1" type="application/pdf"/>
  • Related