Я работаю с пружинным загрузочным сервером с angular4 на первой странице. У меня есть служба для загрузки файла .zip
с моего фронта, используя HttpClient
. Вот мой код:
Угловой сервис:
getZip(fileName: string) : Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/zip',
'Accept': 'application/zip'
}),
params: new HttpParams().set('fileName', fileName),
reponseType: 'blob'
};
return this.http.get<Blob>(extractZip, httpOptions);
}
Угловой сервисный звонок:
this.myService.sendSql(this.sql, this.dataSource, this.fileGeneration).subscribe(data => {
if(this.fileGeneration) {
this.myService.getZip(data.zipName).subscribe(blob => {
console.log(blob);
console.log("Zip file download success.")
},
error => {
console.log(error);
console.log("Zip file download failed.")
});
}
},
err => {
console.log('An error occured when contacting application server.');
});
Так что в основном я использую this.myService.sendSql()
, чтобы получить имя почтового индекса, которое я буду использовать с this.myService.getZip()
для загрузки почтового индекса.
Мой запрос такой: http://localhost:8870/extracts_sql?fileName=name.zip
, и внутри моего браузера он работает отлично.
Здесь код на стороне сервера:
@GetMapping("/extracts_sql")
public ResponseEntity<InputStreamResource> getFile(@RequestParam String fileName) throws FileNotFoundException {
Configuration configuration = ConfigurationHelper.readConfiguration(configurationFile);
MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(this.servletContext, fileName);
File file = new File(configuration.getProcessingFolder() + File.separatorChar + fileName);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
//Resource file = this.sqlToolService.loadFile(fileName, configuration);
log.i("Sending zip :"+fileName+" to user.");
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.contentType(mediaType)
.contentLength(file.length())
.body(resource);
}
Проблема в том, что я получаю HttpErrorResponse
на угловой стороне, хотя его код состояния = 200, вот ошибка: SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse Http failure during parsing for http://localhost:8870/extracts_sql?fileName=name.zip
Есть идеи?