//文件预览js 下载流调用浏览器预览 的方法
var $viewblob=function (url, data) {
const config = { responseType: 'blob', timeout: 9999999 };
$http(url,data,config).then(res => {
const fileRes =res;
let type = { type: 'application/octet-stream' };
const types = {
pdf: { type: 'application/pdf' },
png: { type: 'image/png' },
jpg: { type: 'image/jpeg' },
}
let contentDisposition = res.headers["content-disposition"]||"filename=未知文件";
let patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
let result = patt.exec(contentDisposition);
const filename = decodeURI(contentDisposition).split('=')[1].replace('\'','').replace('"','');
if (filename) {
const suffix = filename.substring(filename.lastIndexOf('.') + 1).toLocaleLowerCase();
if (types[suffix]) { //预览
type = types[suffix];
//其他下载 octet-stream
}
}
let viewurl= window.URL && window.URL.createObjectURL(new Blob([fileRes.blob], type));
if(viewurl){
window.open(viewurl)
}
})
}
标签:const,suffix,预览,filename,let,浏览器,js,type
From: https://www.cnblogs.com/7c89/p/17658790.html