blob数据与json数据互相转换


需求前端获取后端的Excel文件流显示在页面上,当查询条件无数据或接口报错需要$message.error(),需要我们将请求回的

blob数据转换json数据才能抛出提示见代码

 

 

const params = {
                begin_time: this.pickerDate[0],
                end_time: this.pickerDate[1],
                station_uniq: this.siteType
            }
            details_stream(params).then(res => {
                console.log(res)
                // blob数据与json数据互相转换
                if (res.type == 'application/json') {
 let reader = new FileReader() reader.readAsText(res, 'utf-8') reader.onload = e => { let readerres = reader.result let parseObj = {} parseObj = JSON.parse(readerres) let $this = this //this指向问题,如果要将reader.onload=function(e)/reader.onload=e=>{}结果给外界使用必须改变this指向问题 $this.$message.error(parseObj.msg) }
                } else {
                    const objectURL = window.URL.createObjectURL(res) // 链接
                    this.uploadExcel(objectURL)
                }
            })

结果:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM