2016-11-14 1 views
0

Я хочу загрузить файлы с углового 2. Моя проблема в том, что все файлы, которые я получаю от Spring REST API, в два раза больше.Файлы, загруженные с помощью Angular 2 over Spring REST API, имеют два размера. Зачем?

Угловая Отрывок:

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); 

Spring 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

У меня была такая же проблема вчера и она была решена. Я надеюсь, что вы или кто-либо еще можете использовать это решение. Добавьте responseType: ResponseContentType.Blob к вашему запросуОценка authHttp.get вы используете.

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

Затем используйте response.blob() вместо new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})

Для меня, что работал. Надеюсь, это тоже поможет.

Смежные вопросы