2016-11-14 2 views
0

私はアンギュラ2からファイルをダウンロードしたいと思います。私の問題は、Spring REST APIから得られるすべてのファイルがファイルサイズの2倍だということです。Spring REST APIでAngular 2でダウンロードされたファイルは、サイズが2倍です。どうして?

角度スニペット:

return this.authHttp.get(this.restAPI + '/downloadFile/' + fileId) 
     .toPromise() 
      .then(response => { 
       // var arrayBufferView = new Uint8Array(response.arrayBuffer()); 
       // var blob = new Blob([ arrayBufferView ], { type: response.headers.get("Content-Type") }); 
       // return blob; 

       return new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")}) 
       }) 

      .catch(this.handleError); 

春RESTスニペット:

@RequestMapping(value = "/downloadFile2/{fileId}", 
     method = RequestMethod.GET) 
public void downloadFileHandler2(@RequestHeader("Authorization") String token, 
     @PathVariable String fileId, HttpServletResponse response) throws IOException { 

    changeToCustomerDataBase(token); 

    File file = projectService.readFromDB(fileId); 

    response.addHeader("Content-Disposition", "attachment; filename="+file.getName()); 
    response.setContentType(Files.probeContentType(file.toPath())); 
    response.setContentLengthLong(file.length()); 

    FileInputStream fis = new FileInputStream(file); 
    int c; 
    while((c = fis.read()) > -1) { 
     response.getOutputStream().write(c);  
    } 

    response.flushBuffer(); 

    fis.close(); 
} 

ファイル、データベースからのタールは、元のサイズを持っています。

どうしたのですか?私は何かを忘れましたか?私はどんな助けにも非常に感謝しています。

答えて

0

私は同じ問題を昨日持っていて、それを解決しました。あなたや他の誰かがこのソリューションを使用できることを願っています。あなたが使用しているauthHttp.getのrequestOptionsにresponseType: ResponseContentType.Blobを追加してください。

let requestOptions: RequestOptionsArgs = { 
     ..., 
     responseType: ResponseContentType.Blob 
}; 
. 
. 
. 
return this.http.request(path, requestOptions); 

そして、代わりに働いていた私にとってはnew Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})

response.blob()を使用しています。それがあなたにも役立つことを願っています。

関連する問題