使用iframe打开Base64格式的PDF文件显示空白,src的数据也加载出来了,但就是不显示
转换一下思路,如果Base64格式不行的话,把Base64转换成Blob对象,创建一个URL的Blob对象,再将URL赋值给src属性同样可以实现预览
previewPdf(fileBase64){
let fileBlob = this.base64ToBlob(fileBase64,'application/pdf');
let href = window.URL.createObjectURL(fileBlob);
let newWindow = window.open("");
newWindow.document.write("<iframe width='100%' height='100%' src='"+href+"'></iframe>");
},
base64ToBlob(fileBase64,fileType){
let raw = window.atob(fileBase64);
let rawLength = raw.length;
let uint8Array = new Uint8Array(rawLength);
while (rawLength--){
uint8Array[rawLength] = raw.charCodeAt(rawLength);
}
return new Blob([uint8Array],{type: fileType});
}