Answer the question
In order to leave comments, you need to log in
How to send a file along with a form (FormGroup)?
Good day,
How to send files along with the form?
There is a FormGroup
public form: FormGroup;
...
ngOnInit() {
this.form = this.fb.group({
...
file: [],
});
}
<file fromControlName="file"></file>.
...
template: `
...
<input type="file" [(ngModel)]="value" (change)="fileChange($event)" (blur)="onBlur()">Выбрать
...
`
...
public fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
}
}
Answer the question
In order to leave comments, you need to log in
What is called "The answer is on the surface" and "everything ingenious is simple." You just need to do this:
public onSubmit(value: any) {
let params = new FormData();
params.append('file', value.file);
// +другие необходимые параметры
this.testService.testRequest(params).subscribe(res => ...);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question