vue使用axios使用二进制流下载文件

exportData () {
const form = this.getSearchForm() // 要发送到后台的数据
axios({ // 用axios发送post请求
method: ‘post’,
url: ‘/user/12345’, // 请求地址
data: form, // 参数
responseType: ‘blob’ // 表明返回服务器返回的数据类型
})
.then((res) => { // 处理返回的文件流
const content = res
const blob = new Blob([content])
const fileName = ‘123.jpg’
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 中文文档 https://www.kancloud.cn/yunye/axios/234845

猜你喜欢

转载自blog.csdn.net/weixin_43869524/article/details/88688394