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

Kub*_*ski 3 javascript jquery base64

我想打开新窗口(使用jQuery)并从base64内容显示PDF.当我做正常链接时:

<a href=\"data:application/pdf;base64,'.$answer->shipping_label_content.'\" target=\"blank\">Show PDF</a>
Run Code Online (Sandbox Code Playgroud)

都很好.但是我希望用这个内容自动打开新窗口,我不知道如何: - /

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

$(window.document.body).html(html);
Run Code Online (Sandbox Code Playgroud)

Sat*_*pal 7

您可以生成临时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);
Run Code Online (Sandbox Code Playgroud)