// request封装
export function download(params) {
return request({
url:...,
method: 'POST',
responseType: 'blob',
params: params, // 这里也可以使用字面量增强写法
headers:{
‘content-disposition’: 'attachment;filename=*',// filename指定下载文件类型,*代表任意文件类型
‘content-type’: 'application/x-download;charset=utf-8'
}
})
}
//在组件中写点击事件的回调函数
async muDownload(){
const res = await download(params) // params为自己封装的参数
const blob = res.data
let filename = 'test.zip'//这里取个带后缀的名字
if('download' in document.createElement('a')){
//非IE浏览器下载
const elink = document.createElement('a')
elink.download = filename
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) //释放URL对象
document.body.removeChild(elink)
} else {
//IE10+下载
navigator.msSaveBlob(blob,filename)
}
}
axios下载文件的响应函数+携带参数
最新推荐文章于 2022-11-21 00:10:59 发布