1

This works very well when the files are small. As soon as the files get larger (> 1 MB). Can no longer display the PDF.

<embed src="@PDFContent" style="width: 100%;height: 930px;border: none;" frameborder="0" />
PDFContent = "data:application/pdf;base64," + Convert.ToBase64String(File.ReadAllBytes(@"Server:\Temp\test.pdf"));

The file is on a server and not on the IISS. what could be the problem

Welle
  • 13
  • 3

1 Answers1

0

Instead of <embed>, try using <iframe>. Moreover, convert your base64 data back to binary, packed in a Blob and then set your iframe's src to a blob-URI pointing to that Blob -

const blob = base64ToBlob( base64, 'application/pdf' );
const url = URL.createObjectURL( blob );
const pdfWindow = window.open("");
pdfWindow.document.write("<iframe width='100%' height='100%' src='" + url + "'></iframe>");

function base64ToBlob( base64, type = "application/octet-stream" ) {
  const binStr = atob( base64 );
  const len = binStr.length;
  const arr = new Uint8Array(len);
  for (let i = 0; i < len; i++) {
    arr[ i ] = binStr.charCodeAt( i );
  }
  return new Blob( [ arr ], { type: type } );
}

If this approach doesn't work out, try to make your <iframe> point directly to the PDF's URL.

Hope this solution works out for you.

Shrish Sharma
  • 90
  • 1
  • 1
  • 9
  • Thanks, it works great. Is there also a possibility to integrate the iframe into an existing page? I don't want to have a separate window. I've tried the innerHTML of a div, but without success. – Welle Apr 20 '21 at 13:53
  • I'm glad that my solution could be of some help to you. Please consider accepting my Answer as others who might face this problem could make use of it too. – Shrish Sharma Apr 21 '21 at 05:44
  • About using – Shrish Sharma Apr 21 '21 at 05:46