簡體   English   中英

Angular 和 Spring 引導 - 第二次保存 Excel 文件/錯誤“響應不是 Blob”

[英]Angular and Spring Boot - Saving a Excel File / Error "Response is not a Blob" at second time

我有以下問題:我正在嘗試從 REST-API 下載 Excel 文件 (.xlsx)。

應用程序的簡短描述:有項目,可以下載為 Excel 文件及其所有數據。

我第一次下載文件時一切正常。 但是,如果我再次嘗試下載該文件,則會出現“響應不是 blob”錯誤。 我正在使用 Angular 11 和 Spring 引導與 Apache POI。

我設置了一些斷點並注意到,我的后端控制器中的斷點被第二次觸發,所以我認為它是一個前端錯誤(在 Spring-Console 中也沒有錯誤)

這是我的代碼:

// Component (the selected projects ids are emitted to the service)
      exportProjects() {
       const exportIds = this.selection.selected.map(x => x.id);
       this.projectService.downloadSelectedProjects(exportIds);
      }
// Service
  downloadSelectedProjects(selectedProjectIds) {
    let params = new HttpParams();
    params = params.append('selectedProjectIds', selectedProjectIds);
    return this.http.get(this.PROJECT_API_URL + Config.services.projects.exportSelectedProjectListExcel, {
      params: params,
      responseType: 'blob' as 'json' })
      .subscribe(res => {
        let binaryData = [];
        binaryData.push(res);
        let downloadLink = document.createElement('a');
        downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: 'application/octet-stream'}));
        const filename = "test.xlsx";
        downloadLink.setAttribute('download', filename);
        document.body.appendChild(downloadLink);
        downloadLink.click();
    });
  }
// Backend-Controller
    @GetMapping(value = "/exportSelectedProjectListExcel")
    @ResponseBody
    public void exportSelectedProjectListExcel(@RequestParam("selectedProjectIds") List<Long> selectedProjectIds,
                                               HttpServletResponse response) throws IOException {
        try (SXSSFWorkbook xlsx = projectService.getSelectedProjectListExcel(selectedProjectIds)) {
            xlsx.write(response.getOutputStream());
        }
    }

要在 angular 服務中將 excel 數據作為 blob 使用,您的響應類型應為 byteArray ResponseEntity<byte[]>

    @GetMapping(value = "/exportSelectedProjectListExcel")
        @ResponseEntity<OutputStream> exportSelectedProjectListExcel(@RequestParam("selectedProjectIds") List<Long> selectedProjectIds,
                                                   HttpServletResponse response) throws IOException {
    
            try {
    SXSSFWorkbook xlsx = projectService.getSelectedProjectListExcel(selectedProjectIds)
    response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment; filename=File.xlsx");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

xlsx.write(response.getOutputStream());

ByteArrayInputStream stream = new ByteArrayInputStream(outputStream.toByteArray());

IOUtils.copy(stream, response.getOutputStream());
return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=File.xlsx")
            .contentType(new MediaType("application", "octet-stream"))
            .body(outputStream);
      }catch(Exception ex){

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM