I want to implement a file upload (within a reactive form), but $request->files->all()
(from Symfony) is always empty.
My form:
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="col-md-6">
<input type="text" pInputText id="name" formControlName="name">
<label for="name">Name</label>
</div>
<div class="col-md-6">
<input type="file" id="file" (change)="upload($event)">
</div>
// ... remaining form
<button pButton type="button" (click)="onSubmit()" [disabled]="form.invalid" label="Save"></button>
</form>
The relevant TS code:
// Form creation
createForm() {
this.form = this.fb.group({
name: ['', Validators.required],
description: '',
file: [null, Validators.required],
watermark: false
});
}
For uploading:
upload(event) {
let file = event.target.files[0];
this.form.controls['file'].setValue(file);
}
On submit:
onSubmit() {
this.http.post('api/file', this.form.value).subscribe((data) => {
console.log(data);
});
}
Looking at the Request $request
object server-side everything's fine, except the file which is empty: $request->files->get('file')
is always []
.
Anyone got an idea why?