/* 下载附件 */
downloadFile(fileName) {
// window.open(url);
var that = this;
var url = "PO2116"; //接口地址
that.$http
({
url:url + "?filePath=" + fileName,
method: 'post',
headers:{
'Content-Type': 'application/json; application/octet-stream'
},
responseType: 'blob',
})
.then(res => {
console.log(res)
const blob = new Blob([res.data]);
const downloadElement = document.createElement("a");
const href = window.URL.createObjectURL(blob);
//后台再header中传文件名
const name = fileName;
downloadElement.href = href;
downloadElement.download = name;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement); // 下载完成移除元素
window.URL.revokeObjectURL(href); // 释放掉blob对象
});
},
这俩是重点
headers:{
'Content-Type': 'application/json; application/octet-stream'
},
responseType: 'blob',
我单独加一个responseType 下载下来还是乱码 加了header就好了