微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot Angular 8 多文件 zip 下载

如何解决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 举报,一经查实,本站将立刻删除。