Is it possible to send a file with reactive forms(model driven)? Files are from formData and another data is from FormGroup, how to combine this and send to nodejs?
Take file from:
<input formControlName="name" class="form-control" type="text">
<input formControlName="surname" class="form-control" type="text">
<input formControlName="email" class="form-control" type="mail">
<input type="file"(change)="addPhoto($event)" />
Create FormControl
and FormGroup
createFormControls() {
this.name = new FormControl("", Validators.required);
this.surname = new FormControl("", Validators.required);
this.email = new FormControl();
this.file = new FormControl("");
}
createForm() {
this.userData = new FormGroup({
name: this.name,
surname: this.surname,
email: this.email,
file: this.file
});
}
push data
addPhoto(event) {
let target = event.target || event.srcElement;
this.files.push(target.files);
}
send data to node js
onSubmit() {
if (this.userData.valid) {
let filelist: FileList = this.files;
const formData = new FormData();
for (let i = 0; i < filelist.length; i++) {
this.readyFile = filelist[i];
formData.append('file', this.readyFile[0]);
}
// Here I have a main problem - there are "formData" and
// "this.userData.value" how send it to together(concat) ?
this.apiService.updateUserData(--?--)
}
}