发布于 

axios请求pdf文件流并下载保存

接口

设置 responseType: 'blob'

1
2
3
4
5
6
7
function DownloadPdf() {
return request({
url: 'xxxxx',
method: 'get',
responseType: 'blob' // 重点
})
}

保存pdf

1
2
3
4
5
6
7
8
9
const url = URL.createObjectURL(new Blob([data], { type: 'application/pdf;charset-UTF-8' }))
const a = document.createElement('a')
a.style.display = 'none'
a.download = 'fileName'
a.href = url
a.click()
if (document.body.contains(a)) {
document.body.removeChild(a)
}

参考资料