下载文件设置了download属性,但是下载的名称还是乱码,可能是因为下载文件跨域了,解决办法如下
download(file) {
const urls = file.attachLink || file.link
const x = new window.XMLHttpRequest()
x.open('GET', urls, true)
x.responseType = 'blob'
x.onload = () => {
const url = window.URL.createObjectURL(x.response)
const a = document.createElement('a')
a.href = url
a.download = file.name
a.click()
}
x.send()
},
或
this.axios.get('接口地址', { params: params, responseType: 'blob' }).then(res => {
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
document.body.appendChild(a)
a.href = url
a.download = '文件名称'
a.click()
window.URL.revokeObjectURL(url)
})
}