不是使用axios的原因,也不是responseType未设置的原因,仅仅是因为我把请求参数和responseType分成俩对象导致被忽略了…具体get请求传参的可以去看官网,这里记录下因为传参导致我下载文件出的错误。
对比代码放在下面,希望和我碰到同样问题的同学能少踩坑
//正确写法
axios.get(url, { params:{ xxx: 'xx' }, responseType: 'blob' }).then(res => {
const link = document.createElement('a')
let blob = new Blob([res.data])
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
const temp = decodeURI(
res.headers.map['content-disposition'][0]
.split(';')[1]
.split('=')[1]
)
let iconv = require('iconv-lite')
iconv.skipDecodeWarning = true
let filename = iconv.decode(temp, 'UTF-8')
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
//错误写法
axios.get(url, { xxx: 'xx' }, { responseType: 'blob' }).then(res => {
const link = document.createElement('a')
let blob = new Blob([res.data])
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
const temp = decodeURI(
res.headers.map['content-disposition'][0]
.split(';')[1]
.split('=')[1]
)
let iconv = require('iconv-lite')
iconv.skipDecodeWarning = true
let filename = iconv.decode(temp, 'UTF-8')
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})