首先请求接口添加 responseType,声明响应类型

1
2
3
4
5
6
7
export function functionName(year) {
return request({
method: 'get',
url: `/xxxxx/${year}`,
responseType: 'blob' // 使用blob下载
})
}

接收返回值并下载

1
2
3
4
5
6
7
8
9
10
11
12
13
functionName(this.year).then(res=>{
const objectUrl = URL.createObjectURL(
new Blob([res.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
)
const link = document.createElement('a')
link.download = `要下载的文件名` + '.xlsx'
link.style.display = 'none'
link.href = objectUrl
link.click()
document.body.appendChild(link)
})