问题:调用接口后,response是一串乱码,没有以附件形式保存文件。
解决:
//文件下载
export const downloadFile = function (params) {
// return axios.get("/file/fileDownload", { params });
return axios({
method:'get',
url:"/file/fileDownload",
params:params,
responseType:'blob'
}).then(function(res){
if(res.headers['content-disposition'] !== undefined){
let blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
let fileName = decodeURI(res.headers['content-disposition'])
if (fileName) {
fileName = fileName.substring(fileName.indexOf('=') + 1)
}
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
}
})
}