【问题标题】:Problems in Embed PDF content嵌入 PDF 内容的问题
【发布时间】:2021-07-13 15:33:09
【问题描述】:

当文件很小时,这非常有效。一旦文件变大(> 1 MB)。无法再显示 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"));

文件在服务器上,而不是在 IISS 上。 可能是什么问题

【问题讨论】:

    标签: c# html pdf blazor embed


    【解决方案1】:

    尝试使用&lt;iframe&gt;,而不是&lt;embed&gt;。 此外,将您的 base64 数据转换回二进制文件,打包在 Blob 中,然后将 iframe 的 src 设置为指向该 Blob 的 blob-URI -

    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 } );
    }
    

    如果这种方法不起作用,请尝试让您的&lt;iframe&gt; 直接指向 PDF 的 URL。

    希望此解决方案适合您。

    【讨论】:

    • 谢谢,效果很好。是否还有可能将 iframe 集成到现有页面中?我不想有一个单独的窗口。我已经尝试过 div 的 innerHTML,但没有成功。
    • 很高兴我的解决方案能对您有所帮助。请考虑接受我的回答,因为其他可能面临此问题的人也可以使用它。
    • 关于在现有页面中使用
    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 2015-01-06
    • 1970-01-01
    • 2017-04-05
    • 2014-09-20
    • 2019-01-15
    • 2015-06-28
    • 2021-04-09
    相关资源
    最近更新 更多