http http:// localhost:8080/products/download/image001.png的http失败
大家好,我正在构建一个带有弹簧启动和Angular的应用程序,即IM在服务器中上传文件,并且在尝试下载此文件时会遇到此错误“错误:SyntaxError:意外令牌 - 在JSON.PARSE的位置0处的JSON in json in JSON.PARSE”。 这是我的代码的一部分
我的组件
downloadProduct(fileUrl){
this.httpClientService.downProduct(fileUrl).subscribe(response => {
window.open(response.url, '_blank');
}
)
}
我的服务
public downProduct(fileUrl: string | undefined): Observable<any>{
return this.httpClient.get(this.urlBack+"products/download/"+fileUrl, {
responseType: 'json'
});
}
和我的弹簧控制器
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("download/{filename}")
public ResponseEntity<Resource> downloadFiles(@PathVariable("filename") String filename) throws IOException {
System.out.println("weeeeeesh");
Path filePath = get(DIRECTORY).toAbsolutePath().normalize().resolve(filename);
if(!Files.exists(filePath)) {
throw new FileNotFoundException(filename + " was not found on the server");
}
Resource resource = new UrlResource(filePath.toUri());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("File-Name", filename);
httpHeaders.add(CONTENT_DISPOSITION, "attachment;File-Name=" + resource.getFilename());
System.out.println(resource.getFilename());
return ResponseEntity.ok().contentType(MediaType.parseMediaType(Files.probeContentType(filePath)))
.headers(httpHeaders).body(resource);
}
这是控制台中显示的错误
SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9799:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:83895:33) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9798:60) at Zone.runTask (http://localhost:4200/polyfills.js:9571:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:9880:34) at invokeTask (http://localhost:4200/polyfills.js:10993:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:11030:21)
message: "Unexpected token � in JSON at position 0"
stack: "SyntaxError: Unexpected token � in JSON at position 0\n at JSON.parse (<anonymous>)\n at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51)\n at ZoneDelegate.invokeTask (http:/
所以请如何解决此问题
Hello guys im building an application with spring boot and angular, im uploading files in the server and when trying to download this files am getting this error "error: SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse".
Here is part of my code that is concerned
My Component
downloadProduct(fileUrl){
this.httpClientService.downProduct(fileUrl).subscribe(response => {
window.open(response.url, '_blank');
}
)
}
My Service
public downProduct(fileUrl: string | undefined): Observable<any>{
return this.httpClient.get(this.urlBack+"products/download/"+fileUrl, {
responseType: 'json'
});
}
And My Spring Controller
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("download/{filename}")
public ResponseEntity<Resource> downloadFiles(@PathVariable("filename") String filename) throws IOException {
System.out.println("weeeeeesh");
Path filePath = get(DIRECTORY).toAbsolutePath().normalize().resolve(filename);
if(!Files.exists(filePath)) {
throw new FileNotFoundException(filename + " was not found on the server");
}
Resource resource = new UrlResource(filePath.toUri());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("File-Name", filename);
httpHeaders.add(CONTENT_DISPOSITION, "attachment;File-Name=" + resource.getFilename());
System.out.println(resource.getFilename());
return ResponseEntity.ok().contentType(MediaType.parseMediaType(Files.probeContentType(filePath)))
.headers(httpHeaders).body(resource);
}
and here is the error shown in the console
SyntaxError: Unexpected token � in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9799:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:83895:33) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:9798:60) at Zone.runTask (http://localhost:4200/polyfills.js:9571:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:9880:34) at invokeTask (http://localhost:4200/polyfills.js:10993:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:11030:21)
message: "Unexpected token � in JSON at position 0"
stack: "SyntaxError: Unexpected token � in JSON at position 0\n at JSON.parse (<anonymous>)\n at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:123146:51)\n at ZoneDelegate.invokeTask (http:/
so please how can i fix this problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论