简体   繁体   中英

Populating the data attribute of an object tag in Chrome with an ASP.NET MVC action that serves a PDF file result

I have a FileResult action that returns a PDF. I want to embed this PDF in an object tag. When I insert the action into the data attribute of the object tag, like below, no PDF is retrieved or shown in Chrome. (The PDF is shown in Firefox with the Adobe plugin - I don't care about IE.)

<object data="@Url.Action("GetPDF", "PDFCreation", new {id= Model.DocumentId})" type="application/pdf"></object>

It all works otherwise - the object tag works with a direct link to a PDF on the file system (eg, data="~/Content/test.pdf"), and the Action above, if hard-pasted into the location bar, downloads the PDF.

Any thoughts? Thank you!

I managed to get IE to display a Pdf that was thrown back using a FileContentResult and the following object tag

<object>
    <embed src="@Url.Action("GetPDF", "PDFCreation", new {id= Model.DocumentId})" type="application/pdf"></embed>
</object>

Might be worth a try

Fixed it via this answer: Returning a file to View/Download in ASP.NET MVC

Had to append a content disposition header, and set the "Inline" value of the content disposition to true.

var doc = ...
var contentDisposition = new ContentDisposition
{
    FileName = doc.FileName,
    Inline = true
};

Response.AppendHeader("Content-Disposition", contentDisposition.ToString());

return File(doc.Path, MediaTypeNames.Application.Pdf);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM