Angular-SpringBoot下载了Excel文件HttpErrorResponse [英] Angular-SpringBoot downlod excel file HttpErrorResponse

查看:81
本文介绍了Angular-SpringBoot下载了Excel文件HttpErrorResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有倾斜首页的Spring Boot服务器.我可以从前面下载.xlsx文件.

I am working a spring boot server with an angular front page. I have a service to download a .xlsx file from my front.

这是我的代码: 服务器端代码:

Here's my code : server side code :

 @GetMapping("/ExportExcel{date}")
 public ResponseEntity<InputStreamResource> excelExportReport(@RequestParam Date date) throws IOException {

List<InterfaceTable> interfaceTables=interfaceTableRepo.afficheAHT(date);
       ByteArrayInputStream in =ExportExcel.ahtToExcel(interfaceTables);
       HttpHeaders headers = new HttpHeaders();
       headers.add("Content-Disposition", "attachment; filename=customers.xlsx");

return ResponseEntity
             .ok()
             .headers(headers)
             .body(new InputStreamResource(in));      

}
角度服务:

}
Angular service:

 ExportExcel(date:string){
 return this.http.get<Operation[]>(this.exportUrl+date) }

问题在于,即使在HttpErrorResponse位置,我也会得到一个HttpErrorResponse:

The issue is that I get a HttpErrorResponse on the angular side even though its:

错误:语法错误:XMLHttpRequest.onLoad上JSON.parse()位置0处JSON中的意外令牌P( http://localhost:4200/polyfills.js:3240:31 )在对象上

error: SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:9948:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3240:31) at Object`

推荐答案

Spring Controller

 @GetMapping("/ExportExcel/{date}")
 public void excelExportReport(@RequestParam Date date, HttpServletResponse httpServletResponse) throws IOException {

           List<InterfaceTable> interfaceTables=interfaceTableRepo.afficheAHT(date);
           ByteArrayInputStream in =ExportExcel.ahtToExcel(interfaceTables);
           byte[] byteArray = new byte[in.available()];
           try {
               byteArrayInputStream.read(byteArray);
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }

           ByteArrayOutputStream out = new ByteArrayOutputStream(byteArray.length);
           out.write(byteArray, 0, byteArray.length);

           httpServletResponse.setContentType("text/csv");
           httpServletResponse.addHeader("Content-Disposition", "attachment; filename=customers.csv");

           OutputStream os;
           try {
               os = httpServletResponse.getOutputStream();
               out.writeTo(os);
               os.flush();
               os.close();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
    }

前端服务

return this.http.get("/ExportExcel/", this.commonRepository.getRequestOptionsForCSV(URL.BEARER)).map(res => {
          return {
              filename: 'customers.csv',
              data: res.blob()
            };
});

前端组件

this.customerService.generateReportCsv(this.receiptReportPage.value).subscribe((results)=>{
    console.log(results);

    var url = window.URL.createObjectURL(results.data);

    var a = document.createElement('a');
    document.body.appendChild(a);
    a.setAttribute('style', 'display: none');
    a.setAttribute('target', 'blank');
    a.href = url;
    a.download = results.filename;
    a.click();
    window.URL.revokeObjectURL(url);
    a.remove();
});

这篇关于Angular-SpringBoot下载了Excel文件HttpErrorResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆