function downFile(res, fileName) {
const blob = new Blob([res], {
type: `application/pdf;charset=UTF-8`
});
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie11
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
const url = URL.createObjectURL(blob);
const downloadElement = document.createElement("a");
downloadElement.href = url;
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
downloadElement.click();
downloadElement.remove();
URL.revokeObjectURL(url);
}
}
try {
this.$http.post("/api/company/bill/download", {
id,
}, {
responseType: 'blob'
}).then((res) => {
this.showLoading = false;
downFile(res, `123.pdf`)
return;
});
} catch (error) {
console.error('error: ', error);
}
重点在于responseType: ‘blob’,指定已blob形式接收。
接口返回的大致如下: