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