Google 'Angular 8 Springboot File Upload'. I tried the first 3 pages. Couldn't upload a damn file.
Furthest I got was:
Resolved [org.springframework.web.multipart.MultipartException: Current request is not a multipart request]
Tried everything I could.
I just want to send a file from Angular and receive it in a spring boot rest controller. Could someone please help me out with that?
Back End:
@PostMapping("/users/profile/upload")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> uploadData(@RequestParam("file") MultipartFile file)
throws Exception {
if (file == null) {
throw new RuntimeException("You must select the a file for uploading");
}
String originalName = file.getName();
// Do processing with uploaded file data in Service layer
return new ResponseEntity<String>(originalName, HttpStatus.OK);
}
Front end service:
uploadFile(file: File): Observable<any> {
const formData = new FormData();
formData.append('file', file, 'file');
const headers = new HttpHeaders({
'Content-Type': 'multipart/form-data'
});
const options = { headers };
return this.http.post(`${this.baseUrl}/profile/upload`, formData, options);
}