Show byte [] in pdf format with html5

Hi, I have a big problem. I have byte [] receveid from the Wcf service. A byte array represents a PDF file. In the controller, I would like to do this:

PDFDto pdfDTO = new PDFDTO(); pdfDTO.pdfInBytes = pdfInBytes; //the byte array representing pdf return PartialView("PopupPDF,pdfDTO); 

in the "I" view, I would like to do this:

  <object data="@Model.pdfInBytes" width="900" height="600" type="application/pdf"> </object> 

but it seems wrong. I searched all over the Internet, there are many reports of this problem, but no one matches my situation. Do you have any ideas? Thank you very much!

+4
source share
2 answers

I would recommend creating another action on the controller specifically for rendering PDF data . Then it's just a matter of referencing it in your data attribute:

 data="@Url.Action("GetPdf", "PDF")" 

I'm not sure if you can dump raw PDF data in the DOM without affecting any coding problem. You can do as images, although I have never tried. But, for demonstration and image it will look like this:

 src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" 

So, assuming this can be done , it will probably look like this:

 @{ String base64EncodedPdf = System.Convert.ToBase64String(Model.pdfInBytes); } @* ... *@ <object data="data:application/pdf;base64,@base64EncodedPdf" width="900" height="600" type="application/pdf"></object> 

I still support my original answer to create a new action.

+9
source
 protected ActionResult InlineFile(byte[] content, string contentType, string fileDownloadName) { Response.AppendHeader("Content-Disposition", string.Concat("inline; filename=\"", fileDownloadName, "\"")); return File(content, contentType); } 

Add this code to your base controller or the controller itself and call this function to show the file in the browser itself.

0
source

Source: https://habr.com/ru/post/1487883/


All Articles