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)
                }
            })

结果:

 

 

 

posted @ 2021-11-10 15:32  Jim-vue  阅读(2515)  评论(0编辑  收藏  举报