我也是网上抄的复制下面两个方法,直接调用fileLinkToStreamDownload 就能下载
function fileLinkToStreamDownload(url, fileName, type){
let reg = /^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-]+).)+([A-Za-z0-9-/])+{type}`);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
//接受二进制文件流
var blob = this.response;
downloadExportFile(blob, fileName, type)
}
};
xhr.send();
}
}
/**
*下载导出文件
- @param blob :返回数据的blob对象或链接
- @param tagFileName :下载后文件名标记
- @param fileType :文件类 word(docx) excel(xlsx) ppt等
*/
function downloadExportFile(blob, tagFileName, fileType) {
let downloadElement = document.createElement('a');
let href = blob;
if (typeof blob == 'string') {
downloadElement.target = '_blank';
} else {
href = window.URL.createObjectURL(blob); //创建下载的链接
}
downloadElement.href = href;
downloadElement.download = tagFileName + moment(new Date().getTime()).format('YYYYMMDDhhmmss') + '.' + fileType; //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
if (typeof blob != 'string') {
window.URL.revokeObjectURL(href); //释放掉blob对象
}
}