需求前端獲取后端的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) } })
結果: