方法封装
binaryDownload(res){
if(!res.data){
this.$message.error("文件下载失败!");
return false;
}
const blob = new Blob([res.data], {type: 'application/octet-stream', endings: 'transparent'});
let contentDisposition = res.headers['content-disposition'];
let patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
let result = patt.exec(contentDisposition);
let filename = result[1];
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, decodeURI(filename));
}else{
const blobURL = window.URL.createObjectURL(blob);
const aLink = document.createElement('a');
aLink.href = blobURL;
aLink.setAttribute('download', filename);
if (typeof aLink.download === 'undefined') {
aLink.setAttribute('target', '_blank');
}
aLink.style.display = 'none';
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
window.URL.revokeObjectURL(blob);
}
}
调用下载接口
downLoadDocument(row) {
this.$axios.product.productRepositoryFileDown({
params:{
fileName: row.name,
fileType: row.fileType,
id: row.id,
productId: this.storage.productId,
versionId: this.versionId,
userId: this.storage.userId
},
timeout: 0,
responseType: "blob",
headers: {
"Content-Type": "application/json;application/octet-stream"
}
}).then(res => {
this.$utils.binaryDownload(res);
})
}