Spring Boot Angular 8 多文件 zip 下载

问题描述

我正在下载带有 spring boot 和 angular 的多个文件 zip。它与 spring boot postman 一起工作,但是当我执行角度执行时,它会下载这样的文件

error

Spring Boot

@GetMapping("/fileZIP/{personId}")
    public void getAllFile(@PathVariable(value = "personId") Integer personId,HttpServletResponse response) throws FileNotFoundException,IOException {
        List<File> fileList = service.findByFile(personId);
    
    String zipName = person.getName() + "_" + person.Surname() + ".zip";
    FileOutputStream fileOutputStream = new FileOutputStream(zipName);
    ZipOutputStream zipOut = new ZipOutputStream(new bufferedoutputstream(fileOutputStream));
    
    for (File file : fileList) {
        FileSystemResource resource  = new FileSystemResource(file.getFilePath());
        ZipEntry zipEntry = new ZipEntry(resource.getFilename());
        zipEntry.setSize(resource.contentLength());
        zipOut.putNextEntry(zipEntry);
        StreamUtils.copy(resource.getInputStream(),zipOut);
        zipOut.closeEntry();
    }
    zipOut.finish();
    zipOut.close();
    response.setContentType("application/octet-stream");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-disposition",String.format(" attachment; filename=\"%s\"",zipName));
    
}

Angular-component.ts

getFile(event) {
    this.editorBasvurularservice.getFilePerson(event.personId,headers).subscribe(response => {
      let dataType = response.type;
      let binaryData = [];
      binaryData.push(response);
      let downloadLink = document.createElement('a');
      downloadLink.href = window.URL.createObjectURL(new Blob(binaryData,{ type: dataType }));
      
      document.body.appendChild(downloadLink);
      downloadLink.click();
    }
    )

}

Angular-service.ts

getFilePerson(personId: number,headers): Observable<any> {
    return this.http.get(apiHost + '/fileZIP/' + personId,{ headers,responseType: 'arraybuffer' }).pipe(
      map((data: any) => {
        return data;
      })
    );
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)