前端vue文件流怎么下载
时间: 2023-12-23 20:03:13 浏览: 31
你可以使用如下代码实现前端Vue文件流下载:
```
// 定义下载方法
function downloadFile(data, filename) {
const blob = new Blob([data])
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename) // 兼容IE10
} else {
const link = document.createElement('a')
const evt = document.createEvent('MouseEvents')
link.href = window.URL.createObjectURL(blob)
link.download = filename
// 模拟点击实现下载
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
link.dispatchEvent(evt)
// 释放URL对象
window.URL.revokeObjectURL(link.href)
}
}
// 调用下载方法
axios.get('/api/download').then(res => {
downloadFile(res.data, 'example.docx')
})
```
其中,`axios.get('/api/download')`用于获取文件流数据,`downloadFile`方法用于将文件流下载到本地。传参中的`filename`参数为下载的文件名。