前端下载文件(GET、POST方法)

GET

location.href = '/path/to/download/url'

POST

axios.post('/path/to/download/url', this.searchParams, {
responseType: 'blob'
}).then(res => {
let blob = res.data
let reader = new FileReader()
reader.readAsDataURL(blob)
reader.onload = (e) => {
let a = document.createElement('a')
a.download = `表格名称.xlsx`
a.href = e.target.result
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
}).catch(err => {
console.log(err.message)
})

posted @ 2020-05-21 11:40  是大壳子啊  阅读(2163)  评论(0编辑  收藏  举报