I am trying to upload a file on client side and send HTTP Post request to Spring Rest Controller. But when I receive the file object in Rest controller I could see it as 'null'. HTML code :
<input type="file" id="myFile" (change) = "onFileChange($event)" #fileInput>
Corresponding .ts file :
onFileChange($event) : void {
let file:File = $event.target.files[0];
let myReader: FileReader = new FileReader();
myReader.onloadend = function(e) {
}
myReader.readAsText(file);
const req = this.http.post('/abc',myReader.result, {
headers: new HttpHeaders().set('content-type','application/pdf')
});
req.subscribe(
(data) => {
console.log(data);
},
(err: HttpErrorResponse) => {
if(err.error instanceof Error) {
//client side error
console.log('An error occured: ',err.error.message);
} else {
console.log('Backend returned code', err.status, 'body was ',err.error);
}
}
)
}
My Spring Rest Controller :
@RequestMapping(value="/abc", method = RequestMethod.POST, consumes = "application/pdf")
public ResponseEntity<String> uploadFile(@RequestBody MultipartFile file) {
System.out.println("Inside Controller");
return new ResponseEntity<>("true", HttpStatus.CREATED);
}
Could anyone please help to find out the issue here.