I have a somewhat complex form with two nested FormGroup. Each nested FormGroup containing a file input field along with other 6 input fields as Follows:
In the Component:
updateEmployeeNomineeForm: FormGroup;
ngOnInit() {
this.updateEmployeeNomineeForm = this.formBuilder.group({
employeeNominee1: this.formBuilder.group({
employeeId: [employeeId],
nomineeName: [],
nomineeRelation: [],
nomineeContactNo: [],
nomineeNationalId: [],
nomineePicture: [], // This is the file input
additionalInfo: []
}),
employeeNominee2: this.formBuilder.group({
employeeId: [employeeId],
nomineeName: [],
nomineeRelation: [],
nomineeContactNo: [],
nomineeNationalId: [],
nomineePicture: [], // This is the file input
additionalInfo: []
})
});
}
And the HTML form is as like as follows:
<div *ngIf="updateEmployeeNomineeForm">
<form [formGroup]="updateEmployeeNomineeForm" (ngSubmit)="updateEmployeeNominees()">
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<strong>Nominee-1</strong>
</div>
<div formGroupName="employeeNominee1" class="card-body">
//Other inputs omitted for Clarity
<div class="form-group row">
<label class="col-md-4 col-form-label">Nominee Picture</label>
<div class="custom-file col-md-8">
<input type="file" #nomineePicture1 formControlName="nomineePicture" class="form-control">
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
<strong>Nominee-2</strong>
</div>
<div formGroupName="employeeNominee2" class="card-body">
//Other inputs omitted for Clarity
<div class="form-group row">
<label class="col-md-4 col-form-label">Nominee Picture</label>
<div class="custom-file col-md-8">
<input type="file" #nomineePicture2 formControlName="nomineePicture" class="form-control">
</div>
</div>
</div>
</div>
</div>
</div>
<br />
<div class="text-center">
<button type="submit" [disabled]="!updateEmployeeNomineeForm.valid" class="btn btn-success text-white"><i class="fa fa-floppy-o" aria-hidden="true"></i> Submit</button>
</form>
</div>
Now the updateEmployeeNominees() method in the Component as follows:
updateEmployeeNominees(): void {
this.employeeService.updateEmployeeNominees(this.updateEmployeeNomineeForm.value).subscribe((updateStatus) => {
if (updateStatus) {
this.resetUpdateEmployeeNominees();
this.updateSuccessMessage = "Employee Nominees updated successfully!";
setTimeout(() => {
this.updateSuccessMessage = null;
}, 4000);
}
}, (error) => {
this.serverErrorMessage = this.errorMessageService.getServerErrorMessageText();
});
}
Now the updateEmployeeNominees()
method in the EmployeeService as Follows:
updateEmployeeNominees(employeeNominees: any): Observable<any> {
const body = JSON.stringify(employeeNominees);
const headerOptions = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.put<any>(this.baseUrl + 'UpdateEmployeeNominees/'+ employeeId, body, {
headers: headerOptions
}).catch(this.handleError);
}
In the ASP.NET Core Web Api Controller:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateEmployeeNominees([FromRoute] int id,[FromBody] EmployeeNomineesViewModel employeeNominees)
{
//Necessary operation goes here
return Ok(true);
}
Everything works as expected except the nomineePictures upload..Would anybody help me to bind the image files to the input field while posting the form to ASP.NET Web API controller method.