如何使用Base64 PDF内容打开新窗口?

4
我希望能够用jQuery打开一个新窗口,并显示base64内容的PDF。当我创建常规链接时:
<a href=\"data:application/pdf;base64,'.$answer->shipping_label_content.'\" target=\"blank\">Show PDF</a>

一切都很好。但是我想要自动打开一个新窗口并显示这个内容,我不知道怎么做:-/

var window = window.open();
var html = "data:application/pdf;base64,'.$answer->shipping_label_content.'";

$(window.document.body).html(html);
1个回答

8
您可以生成一个临时的 anchor 并以编程方式进行单击。
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = "data:application/pdf;base64,'.$answer->shipping_label_content.'";

//Set properties as you wise
link.download = "SomeName";
link.target = "blank";

//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

这个工作类似于 window.open("data:application/pdf;base64,'.$answer->shipping_label_content.'", "_blank", ''); 但Safari阻止了第一和第二个 :-/ - Kuba Żukowski
这个解决方案在IE上不起作用。有没有其他的解决方案可以让它在IE上工作? - Siddharth_Vyas
@Siddharth_Vyas,你可以看一下FileSaver - Satpal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接