2017-08-18 90 views
2

我試圖下載任何文件調用我的其餘webservices。我使用的是Web服務的Spring +球衣和前端的Angular 2。 所以當我碰到前面的時候,webservices得到我的文件,但是下載它的窗口沒有顯示。從其他webservices下載文件彈簧

我休息API:

@POST 
    @Path("/download") 
    @ApiOperation(value = "Download") 
    @Produces(MediaType.APPLICATION_OCTET_STREAM) 
    public Response downloadFile(@ApiParam(value = "File", required = true) String filePath) { 
     File file = new File("/webapps/pdf/upload/msg/1/gest.PNG"); 
     Response.ResponseBuilder response = Response.ok((Object) file); 
     try { 
      String contentType = Files.probeContentType(file.toPath()); 
      response.header("Content-Disposition", "attachment; filename="+file.getName()); 
      response.header("Content-Type", contentType); 
      return response.build(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

我的角度服務:

downloadFile(path) { 
     const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*'}); 
     const options = new RequestOptions({headers: headers}); 

     options.responseType = ResponseContentType.Blob; 
     return this.http.post(apiUrl + "msg/download", path, options) 
     .catch(this.handleError); 
    } 

我的角度成分:

downloadFile(documentPath) { 
    this.msgService.downloadFile(documentPath).subscribe(response => { 
     var contentType = response.headers('Content-Type'); 
     let url = window.URL.createObjectURL(new Blob([response._body], {type: contentType})); 
     window.open(url);  
     }); 
    } 

HTML:

<figure class="ui-g-12 " *ngFor="let document of msg.documents_path" (click)="downloadFile(document)"> 
     <img [src]="selectImageByExtension(document.split('.').pop().toLowerCase())" /> 
     <figcaption>{{document.split('/').pop().toLowerCase()}}</figcaption> 
</figure> 

當我點擊我的figure我可以看到,該文件也得到: enter image description here

但沒有彈出。 我錯過了什麼?

+0

你檢查日誌,看看是否WS接到電話並沒有遇到IOException異常? – Assafs

+0

是的,它可以工作,否則我無法從調試視圖中獲取它 – anais1477

回答

0

所以唯一對我有用的解決方案是使用GET請求,而不是POST將文件路徑作爲pathparam傳遞。

REST API:

@GET 
    @Path("/download/{filePath}") 

    @Produces(MediaType.APPLICATION_OCTET_STREAM) 
    public Response getdownloadFile(@PathParam("filePath") String filePath) { 
     String path = null; 

      byte [] barr = Base64.getDecoder().decode(filePath); 
      path = new String(barr); 
     File file = new File(path); 
     try { 
      String contentType = Files.probeContentType(file.toPath()); 

      Response.ResponseBuilder response = Response.ok((Object) file); 
      response.header("Content-Disposition", "attachment; filename="+file.getName()); 
      response.header("Content-Type", contentType); 
      response.header("Content-Length", file.length()); 
      return response.build(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); 
     } 
    } 

角服務:

downloadFile(path) { 
    const headers = new Headers({'Content-Type': 'text/plain', 'Accept': '*'}); 
    const options = new RequestOptions({headers: headers}); 
    options.responseType = ResponseContentType.Blob; 
    return this.http.get(apiUrl + "msg/download/"+btoa(path), options) 
     .map(res => res) 
     .catch(this.handleError); 
    } 

角度成分:

downloadFile(documentPath) { 
    this.msgService.downloadFile(documentPath).subscribe(response => { 
     let params = documentPath.split('/'); 
     var blob = new Blob([response._body]); 
     FileSaver.saveAs(blob, params[params.length-1]); 
     }); 
    }