File upload code referred to from here.
Issue is that it sending request as json not as multipart form data, so the server side code rejects the request and upload fails.
Server side code is fine, I tried that from postman, it works !
This is the html part
<h1>Angular 10 FormData (multipart/data-form) Example</h1>
<div>
<form [formGroup] = "uploadForm" (ngSubmit)="onSubmit()">
<div>
<input type="file" name="profile" (change)="onFileSelect($event)" />
</div>
<div>
<button type="submit">Upload</button>
</div>
</form>
</div>
This is the component
onSubmit() {
const formData = new FormData();
formData.append('file', this.uploadForm.get('profile').value);
this.http.post<any>(CommonConstants.env+"api/master/upload",formData).subscribe(
(res) => console.log(res),
(err) => console.log(err)
);
}
onFileSelect(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0];
this.uploadForm.get('profile').setValue(file);
}
}
Once I uploaded, the network tab in chrome does not show Content-Type header in Request Headers.