vue3封装axios通用文件下载
时间: 2023-11-12 11:06:24 浏览: 108
vue.js和axios.js的下载
好的,以下是vue3封装axios通用文件下载的代码示例:
```javascript
import axios from 'axios'
export function downloadFile(url, params) {
axios({
method: 'get',
url: url,
params: params,
responseType: 'blob'
}).then(res => {
const content = res.data
const fileName = res.headers['content-disposition'].split(';')[1].split('filename=')[1].replace(/"/g, '')
const blob = new Blob([content])
if ('download' in document.createElement('a')) {
const link = document.createElement('a')
link.download = fileName
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
} else {
navigator.msSaveBlob(blob, fileName)
}
})
}
```
阅读全文