首页 > 解决方案 > 如何在Angular 8中使用base64 pdf字符串显示pdf?

问题描述

我正在使用 Angular 8 获取带有 iframe 的 base64 pdf 字符串以在新选项卡中打开,但它没有在 Internet Explorer 中打开,在 IE 中显示空白页面。所以我不想使用 iframe 打开 pdf。谁能帮我在 IE 中显示 base64 pdf 字符串。

标签: javascripthtmlangulartypescript

解决方案


萨拉姆伴侣,

您应该首先将 Base64 转换为 blob,您可以按照此问题中的说明进行操作:

在 JavaScript 中从 Base64 字符串创建 BLOB

然后您应该使用 [window.URL.createObjectURL(fileblob)] 将您的 blob 转换为 URL,然后创建锚点并单击它,如下所示:

  var fileblob = b64toBlob('<your_base64>', 'application/pdf');
  var url = window.URL.createObjectURL(fileblob); 

  let anchor = document.createElement("a");
  anchor.href = url;
  anchor.target = "_blank"
  anchor.click();

推荐阅读