javascript 在 chrome 新版本上使用 window.open 显示 base64 pdf 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46702929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show base64 pdf data using window.open on chrome new version
提问by Gitaram Kanawade
I am using following code for open base64 data as pdf in new window
我正在使用以下代码在新窗口中以 pdf 格式打开 base64 数据
var pdf=response.data.base64;
var doc = document.createElement("a");
doc.href ='data:application/octet-stream;base64,' + pdf;
doc.target = "blank";
doc.click();
$window.open('data:application/pdf;base64,' + pdf);
This is working fine for chrome Version 56.0.2924.87
but not working in version 61.0.3163.100 [Refer screenshot]
Sample plunker code
这适用于 chrome 版本 56.0.2924.87,但不适用于 61.0.3163.100 版 [参考屏幕截图]
示例 plunker 代码
回答by harold
var pdfResult = data_.data.info.result.result;
let pdfWindow = window.open("")
pdfWindow.document.write("<iframe width='100%' height='100%' src='data:application/pdf;base64, " + encodeURI(pdfResult) + "'></iframe>")
this serves to display the base64 pdf in a browser tab.
这用于在浏览器选项卡中显示 base64 pdf。
回答by kleiser sarifo
I faced similar issue using AngularJS. This is how I have managed, in my case I load the file from URL as arraybuffer. Hope it helps.
我在使用 AngularJS 时遇到了类似的问题。这就是我的管理方式,在我的情况下,我从 URL 加载文件作为数组缓冲区。希望能帮助到你。
$http({
method: 'GET',
url: '/api/transaction-file/'+id,
responseType:'arraybuffer'
}).then(function(response){
var file = new Blob([response.data], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
}, function(response){
//Error
});
回答by Thiago Queiroz
I have me too problem and my solution with my friend Petrus is:
我也有问题,我和朋友 Petrus 的解决方案是:
const win = window.open("","_blank");
let html = '';
html += '<html>';
html += '<body style="margin:0!important">';
html += '<embed width="100%" height="100%" src="data:application/pdf;base64,'+base64+'" type="application/pdf" />';
html += '</body>';
html += '</html>';
setTimeout(() => {
win.document.write(html);
}, 0);


