zoukankan      html  css  js  c++  java
  • 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)
                    }
                })

    结果:

  • 相关阅读:
    【一起来烧脑】读懂HTTP知识体系
    Day16:小前端
    Day15:大前端
    Day14:CSS垂直居中
    Day13:H5+JS+C3
    web学习
    javascript 闭包
    微信小程序微信登录
    微信小程序开源
    mpvue搭建小程序框架
  • 原文地址:https://www.cnblogs.com/huoshengmiao/p/15533745.html
Copyright © 2011-2022 走看看