前端原生js解析base64编码
作者:互联网
解析base64编码
let base = result.data.report; //获取接口返回的数据值
let newUrl = base.replace(/[\n\r]/g, '');
let bstr = window.atob(newUrl);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
//确定解析格式
let blob = new Blob([u8arr], {type: 'application/pdf;base64'});
let url = window.URL.createObjectURL(blob)
//在新窗口打开该pdf用这个
window.open(url)
解析base64编码、查看 下载操作
let base=''//你要传入的base64数据
let bstr = atob(base64)
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {u8arr[n] = bstr.charCodeAt(n);}
//确定解析格式,可能可以变成img,没有深入研究
let blob = new Blob([u8arr], {type: 'application/pdf;chartset=UTF-8'});
let url = window.URL.createObjectURL(blob)
//在新窗口打开该pdf用这个
window.open(url)
//下载dpf用这个
const a = document.createElement('a')
a.href = url
a.download = name + '.pdf'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
//删除url绑定
window.URL.revokeObjectURL(url)
标签:原生,url,base64,js,window,let,bstr,u8arr 来源: https://blog.csdn.net/qq_48989313/article/details/120287354