this.$axios
.post(url接口地址, params请求参数, {
headers: {
token: token
},
responseType: "arraybuffer"
})
.then((file) => {
let content = file.data;
// 组装a标签
let elink = document.createElement("a");
// 设置下载文件名
elink.download = "附件.zip";
elink.style.display = "none";
let blob = new Blob([content], {type: "application/zip"})
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
})
注意:responseType应设置为:'arraybuffer',这样返回的文件流才会是二进制的,才能使用new Blob得到正确的文件