Hay, my angular project have user register form, in that i want to upload user image and enter data push to web api. In that case i will push user entered data to json object. So i want to push upload file data and as well as other data to object body.
In separate function i will call to upload file, that's work fine.(i will do it for testing purpose, but i want to push as one object with all data)
Angular 7 cli
registed-page.component.ts
onSubmit(form:NgForm){
var customerData = {
"cusName": form.value.userName,
"cusPassword": form.value.password,
"cusEmail": form.value.email,
"cusImage" : ############
};
//OTHER CODE TO PUSH THAT JSON OBJECT TO WEB API-------
}
//for image upload ----------------------------------------
public uploadFile (getData){
if (getData.length === 0) {
return;
}
let fileToUpload = <File>getData[0];
const ImageData = new FormData();
ImageData.append('file', fileToUpload, fileToUpload.name);
this.http.post('https://localhost:44301/api/Customer/CustomerFileUpload', ImageData)
.subscribe(event => {
console.log("imae upload ----> "+event)
});
}
//for image upload ----------------------------------------
register-page-component.html
<form (ngSubmit)="onSubmit(formData)" #formData="ngForm" >
<input #file type="file" name="file" >
<input
(click)="uploadFile(file.files)"
class="btn btn-large btn-lg btn-circle"
type="submit">
</form>