下载blob流乱码主要原因是因为blob流不能放在axios的header里面而是放在外面
_this.$send.get({
url: '/attendance/exportExcelTemplate',
responseType: 'blob',
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Content-Type': 'application/json; charset=UTF-8',
'Accept-Encoding': 'gzip, deflate',
}
}, res => {
const blob = new Blob([res], {type: 'application/vnd.ms-excel;charset=utf-8;'})
let url = window.URL.createObjectURL(blob)
const fileName = 'attendanceTemplate.xlsx'
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = window.URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
// window.open(url)
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
})
_this.$send.post({
url: '/attendance/exportExcel',
responseType: 'blob',
headers: {
Accept: 'application/ms-excel',
'Content-Type': 'application/json;charset=UTF-8',
},
data: data
}, res => {
const blob = new Blob([res], {type:"application/vnd.ms-excel"})
console.log(blob)
const fileName = 'attendanceTemplate.xlsx'
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = window.URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
});
而关键点就是axios里面要这样放在外面
axios({
method: defaults.method,
url: defaults.url,
data: defaults.method === 'POST' || defaults.method === 'PUT' ? defaults.data : null,
params: defaults.method === 'GET' || defaults.method === 'DELETE' ? defaults.data : null,
headers: defaults.headers,
baseURL: defaults.baseURL,
responseType: defaults.responseType,//这个network里面header是找不到的这才是对的
})