js如何下载后端返回的二进制文件

js如何下载后端返回的二进制文件,第1张

async downloadFile() {

const cname = this.loginUser.username

const params = await this.getParams()//自己写的举升裂获取参数方法

axios({

url: ${baseURL}/download ,

method: 'post',

data: params,

responseType: 'blob',

headers: {

cid: this.customerCode

}

}).then(res =>{

const fileName = wifi预笑枯警_${cname}.csv

var blob = res.data

if ('msSaveOrOpenBlob' in navigator) {

window.navigator.msSaveOrOpenBlob(blob, fileName) //IE导出

} else {

let url = window.URL.createObjectURL(new Blob([blob]))

let link = document.createElement('a')

link.style.display = '正闭none'

link.href = url

link.setAttribute('download', fileName)

link.click()

}

})

},

使用FileReader对象,web应用程序团袜可以异步的读取存储在用户计算机上的文件(或闹或竖者原始数据缓冲液大)内容,可以使用File对象或者Blob对象来指定所要处理的文件或数据.

readAsBinaryString()

读取指定的Blob对象或File对象中的内容. 当读取 *** 作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含所读取文件的原始二进制数据.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8164054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存