【发布时间】:2019-12-18 12:37:33
【问题描述】:
我想将文件从 Angular 上传到 Spring。我试过这个:
@PostMapping(path = "/upload", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> uploadData(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws Exception {
......
return new ResponseEntity<>(originalName, HttpStatus.OK);
}
角度代码:
const formData = new FormData();
formData.append('file', file, file.name);
return this.http.post(environment.api.urls.merchants.uploadLogo, formData);
但我得到错误:
message: "Unexpected token S in JSON at position 0"
stack: "SyntaxError: Unexpected token S in JSON at position 0↵ at JSON.parse (<
我认为 Spring 必须返回 JSON 响应,但由于某种原因它不起作用。
你知道我必须如何正确配置 Spring 端点吗?
编辑我试过这个:
@PostMapping(path = "/upload", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<String> uploadData(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws Exception {
......
return new ResponseEntity<>(originalName, HttpStatus.OK);
}
【问题讨论】:
标签: java angular spring spring-boot angular8