Background: When the export function is implemented, the back end returns the binary file stream. When the export fails and there is an error message, we find that we can’t prompt the message returned by the back end. So we find FileReader to solve this problem. 1. Understand FileReader definition FileReader allows Web programs to read files, so you can use this to transform BLOB data. Now, how does this work
exportReport () { var params = this.$refs.query.handleParams(); afterSale.downloadLocalFirstInsRateList({ filters: params, pageIndex: this.pageNum, pageSize: this.pageSize, sorts: [] }).then(res => { let that = this; this.sameParams = res; let reader = new FileReader(); reader.readAsText(res); reader.onload = function (res) { if (res.target.result.indexOf('statusCode') ! == -1) { if (JSON.parse(res.target.result).statusCode === 500) { that.$Message.error(JSON.parse(res.target.result).message); }} else {const fileName = 'local.xlsx '; const link = document.createElement('a'); link.href = window.URL.createObjectURL(that.sameParams) link.download = fileName; link.click() window.URL.revokeObjectURL(link.href); }}; }).catch(error => { console.log(error); this.loading = false; }); },
New FileReader (); ReadaText (); The callback function onload, which gets res.target.result on a successful read, converts the BLOB data into JSON format.