我正在使用带有angular4首页的spring boot服务器。我有一项服务可以使用.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);
}
Angular服务电话:
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()
下载zip的zip名称。
我的请求是这样的: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);
}
问题是,即使状态码= 200,我在角度方面得到HttpErrorResponse
,这是错误: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
有什么想法吗?
答案 0 :(得分:-1)
返回此:
return this.http.get<Blob>(extractZip, httpOptions)