导出Excel,下载文件,返回文件流和报错信息处理

发布时间 2023-05-26 17:16:14作者: everseven
  downloadExcelCreateA (resData, fileName) { // 下载文件
            var blob = new Blob([resData],{ type: 'application/vnd.ms-excel' })
            var downloadElement = document.createElement('a');
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = fileName; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象 
        },
   downloadPreExcelBlob () {
        //   return  console.log(this.searchForm, 'searchform')
            let self = this
            self.fullscreenLoading = true;
            let load = esp.url
            this.$axios({
                url: load,
                params: this.searchForm,
                method: 'get',
                responseType: 'blob',
            })
            .then(function(response) {// 成功
                const resData = response.data
                const fileReader = new FileReader()
                fileReader.onloadend = () => {
                    if (resData.type === 'application/json') {
                    let jsonData = JSON.parse(fileReader.result) // 说明是普通对象数据,后台转换失败
                    // 后台信息
                    // console.log(jsonData)
                    let type = 'error'
                    jsonData.code == 200 ? type = 'success' : 'error'
                    self.$message({
                        message: jsonData.data || '系统维护中,稍后再来吧!',
                        showClose: true,
                        type: type,
                        duration: 3000
                    });
                } else {
                    // 下载文件
                    self.downloadExcelCreateA(resData, '预约导出.xls')
                    self.fullscreenLoading = false;
                }}
                // console.log(resData)
                fileReader.readAsText(resData)
                self.fullscreenLoading = false;
            }).catch(function(error) {
                self.fullscreenLoading = false;
                console.log(error)
            });
        },