I want to upload a file from Angular to Spring. I tried this:
@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);
}
Angular code:
const formData = new FormData();
formData.append('file', file, file.name);
return this.http.post(environment.api.urls.merchants.uploadLogo, formData);
But I get error:
message: "Unexpected token S in JSON at position 0"
stack: "SyntaxError: Unexpected token S in JSON at position 0↵ at JSON.parse (<
I suppose that Spring has to return JSON response, but for some reason it's not working.
Do you know how I have to configure the Spring endpoint properly?
EDIT I tried this:
@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);
}