写一个vue下载文件流的方法
时间: 2024-06-13 18:05:00 浏览: 15
以下是一个Vue下载文件流的方法,使用axios进行请求,设置responseType为blob,然后创建一个Blob对象并使用URL.createObjectURL()方法创建一个URL,最后使用a标签的download属性和click()方法进行下载。
```javascript
<template>
<div>
<button @click="downloadFile">下载文件</button>
</div>
</template>
<script>
import axios from 'axios'
export default {
methods: {
async downloadFile() {
try {
const response = await axios.post('your_download_url', {data: 'your_data'}, {responseType: 'blob'})
const url = URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', 'your_file_name')
document.body.appendChild(link)
link.click()
} catch (error) {
console.error(error)
}
}
}
}
</script>
```