后台輸出流到前端
//有錯誤信息,就導出文件 if (errorList != null && errorList.size() > 0) { String title = "導入國產葯數據的異常數據"; ExcelUtils.exportExcel(errorList, title, title, StandardCode.class, title, response); return null; }
在響應攔截器中加以下代碼:
if (headers['content-type'].toLowerCase() === 'application/vnd.ms-excel;charset=utf-8') { return response }
如果不加這段代碼會報錯:
No converter for [class com.ljxx.entity.Result] with preset Content-Type 'application/vnd.ms-excel;charset=UTF-8'
由於上述代碼返回的是response,故使用filedownload下載的時候應該為response.data
handleUploadChange1(file) { if (file.name.lastIndexOf('.') < 0) { this.$message.error('上傳文件只能是xls、xlsx格式!') return } const testMsg = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase() const extensionXLS = testMsg == 'xls' const extensionXLSX = testMsg == 'xlsx' if (!extensionXLS && !extensionXLSX) { this.$message.error('上傳文件只能是xls、xlsx格式!') return } const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error('上傳文件不能超過 2MB!') return } console.log('import continue') this.importLoading = true this.importDisabled = true const data = new FormData() data.append('file', file.raw) standardCode.importDomesticExcel(data).then(response => { console.log(response) if (response.success) { this.open2(response.msg) this.importLoading = false this.importDisabled = false this.getList() } else { this.$message.success("部分數據導入失敗,數據已下載到本地,請查看!") fileDownload(response.data, '導入數據中的異常數據.xlsx') // this.open2(response.msg) this.importLoading = false this.importDisabled = false } }).catch(() => { this.open2('抱歉,導入失敗') this.importLoading = false this.importDisabled = false }) },
如果報錯文件打不開,如下:
是因為responseType值應為arraybuffer,而不能是arraybuffer;charset=utf-8
importDomesticExcel(data) { return request({ url: '/standardCode/import', method: 'post', data: data, responseType: 'arraybuffer', headers: { 'Content-Type': 'multipart/form-data' } }) },