downloadFile () {
const _this = this
axios({
url: this.url,
method: this.method,
responseType: 'blob',
params: this.method === 'get' ? this.data : undefined,
data: this.method === 'post' ? this.data : undefined
}).then(res => {
const fileData = res.data
const fileReader = new FileReader()
fileReader.onload = function () {
try {
const jsonData = JSON.parse(this.result)
if (jsonData.error) {
throw new Error(`${jsonData.error.message}`)
}
} catch {
// 解析成对象失败,说明是正常的文件流
const content = res.data
// 构造一个blob对象来处理数据
const blob = new Blob([content])
const link = document.createElement('a')
// 获取headers中的filename文件名
const temp = res.headers['co
文件流下载(blob)
最新推荐文章于 2024-08-04 16:33:59 发布
本文详细介绍了如何在JavaScript中利用Blob对象处理文件流,实现前端无刷新、动态下载大文件的功能。通过创建Blob对象,结合URL.createObjectURL方法生成临时下载链接,再触发浏览器的下载行为,可以有效地避免内存压力并提升用户体验。
摘要由CSDN通过智能技术生成