1.首先创立一个HTTP申请
let xhr = new XMLHttpRequest();
xhr.open('get', url, true); //url为地址链接
xhr.setRequestHeader('Content-Type', `application/${type}`);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
//承受二进制文件流
var res = this.response;
downloadExportFile(res, fileName, type)
}
}
xhr.send();
}
@param res :文件链接
@param fileName :文件名;
@param type :文件类型;
2.创立blob
function downloadExportFile (res,fileName,type) {
const blob = new Blob([res],{type:'application/${type}'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = fileName + '.pdf';
a.click();
URL.revokeObjectURL(a.href);
a.remove();
}
这样就实现blob下载文件性能了。
公布到测试环境后发现点击后会报错:Mixed Content: The page at..HTTPS中蕴含http的不平安问题,解决方案:
在html页面中增加
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
发表回复