Is it possible to pass the form data along with image file to web api from angular 6 application.
app.component.ts
onSelectFile(event) {
if (event.target.files && event.target.files[0]) {
this.imageToUpload = event.target.files[0];
const reader = new FileReader();
reader.onload = e => this.selectedImage = reader.result.toString();
this.fileName = event.target.files[0].name;
reader.readAsDataURL(this.imageToUpload);
}
}
createNewComitteeMember() {
var mServiceObject = {
ComitteeMemberName: this.comittee_Member_Name.value,
ComitteeMemberNumber: this.comittee_Member_Number.value,
ComitteeMemberType: this.comittee_Type.value,
ComitteeMemberTypeOthers: this.comittee_Type_Others.value,
ComitteeMemberPosition: this.comittee_Member_Position.value,
ComitteeMemberPositionOthers: this.comittee_Member_Position_Others.value,
ComitteeMemberStatus: this.comittee_Member_Status.value
}
this.dmlService.CreateNewComitteeMember(mServiceObject, this.imageToUpload ).subscribe(data => {
console.log(data);
});
}
service.ts
CreateNewComitteeMember(mFormData,mImage){
// here how can I merge the mFormData and mImage and pass it to the web API
}
can anyone help me to solve this .
You can make use of FormData over here
CreateNewComitteeMember(mFormData,mImage){
const HttpUploadOptions = {
headers: new HttpHeaders({ "Content-Type": "multipart/form-data"})
}
const formData = new FormData();
formData.append('data', mFormData);
formData.append('image', mImage);
return this.httpClient.post(url, formData, HttpUploadOptions)
}
For more info about FormData