首先在请求里加上responseType: 'blob'
http({
url: '',
method: 'post',
headers: {
'Content-Type': 'application/json'
},
responseType: 'blob',
data: saveObj
})
然后再返参里下载
http({
url: '',
method: 'post',
headers: {
'Content-Type': 'application/json'
},
responseType: 'blob',
data: saveObj
}).then(res => {
const currentDate = new Date()
const year = currentDate.getFullYear()
const month = currentDate.getMonth() + 1
const day = currentDate.getDate()
const formattedDate = `${fileName}导入数据${year}_${month}_${day}`
const blob = new Blob([res])
// 开始下载
const elink = document.createElement('a')
elink.download = `${formattedDate}.xlsx`
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
})