【问题标题】:File Download Error(service angular spring boot) [closed]文件下载错误(服务角度弹簧启动)
【发布时间】:2021-06-06 15:55:43
【问题描述】:

使用 angular8 和 spring boot 下载文件。 Spring Boot 服务部分与 Postman 合作。但是当我用 Angular 下载时,我得到一个错误。error

它没有出现在console.log(“文件响应:”,响应)部分。

 downloadFile(event) {
        this.additionalDocumentService.getFileDownload(event.fileId).subscribe(response => {
          console.log("File response:",response)
          this.downloadFile = response;
    
          
        });
      }
      

   getFileDownload(fileId: number): Observable<any> {
    return this.http.get(apiHost + '/downloadFile/' + fileId);

  }

@RequestMapping("/downloadFile/{dosyaId}")
        public ResponseEntity<HttpStatus> handleFileDownloadPage(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "fileId") Integer fileId) throws IOException, Exception {
    
           
            File file = fileService.getFileId(fileId);
            
            if (StringUtils.hasText(dosya.getFilePath())) {
    
                ServletOutputStream out = response.getOutputStream();
    
                InputStream stream = null;
                try {
    -
                    stream = new FileInputStream(file.getFilePath());
                   
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    response.setContentType("application/octet-stream");
                    response.setHeader("Content-Disposition", String.format(" attachment; filename=\"%s\"", file.getFileName()));
    
                    while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                        out.write(buffer, 0, bytesRead);
                    }
                    out.flush();
    
                } catch (Exception e) {
                    System.err.println(e.toString());
                } finally {
                    out.close();
                    if (stream != null) {
                        stream.close();
                    }
                }
                return null;
    
            }
            return ResponseEntity.ok(HttpStatus.OK);
        }

【问题讨论】:

  • 你可以在问题中添加getDosyaIndir方法吗?
  • getDosyaIndir =getFileDownload soryy 有一个拼写错误。

标签: angular spring-boot file download downloadfile


【解决方案1】:

以下函数将接受任何文件类型并弹出下载窗口:

downloadFile(route: string, filename: string = null): void{

    const baseUrl = 'http://myserver/index.php/api';
    const token = 'my JWT';
    const headers = new HttpHeaders().set('authorization','Bearer '+token);
    this.http.get(baseUrl + route,{headers, responseType: 'blob' as 'json'}).subscribe(
        (response: any) =>{
            let dataType = response.type;
            let binaryData = [];
            binaryData.push(response);
            let downloadLink = document.createElement('a');
            downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: dataType}));
            if (filename)
                downloadLink.setAttribute('download', filename);
            document.body.appendChild(downloadLink);
            downloadLink.click();
        }
    )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 2020-11-01
    • 2019-12-15
    • 2019-07-14
    • 2020-09-04
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多