7

I am using primeng for template. And I am using p-fileupload component, but I need to use it in reactive form, so I want to know where to put formControlName as I don't get any input or something label to add.

I only see it with input and put a identifier on it, but i want to know if it is possible on this component.

Any suggestions?

Thanks, in advice!!

1 Answer 1

15

First create your formGroup (reactive form)

this.form = new FormGroup({
  title: new FormControl(""),
  image: new FormControl(null)
});

image is your image component (upload). So then inside your html:

<input type="file" #filePicker (change)="onImagePicked($event)">

So you can hide your input and simulate a click on it. But the onImagePicked function is important. Then back to the code behind:

onImagePicked(event: Event) {
  const file = (event.target as HTMLInputElement).files[0]; // Here we use only the first file (single file)
  this.form.patchValue({ image: file});
}

And yes, that's all! You can use validators as well. All as normal FormControls.

2
  • 1
    What purpose does the template variable filePicker serve here?
    – maurice
    Commented Sep 5, 2024 at 17:34
  • 1
    If you hide the input (display: none) you can create a custom button and then call filePicker.click() to start handling the file upload.
    – Flo
    Commented Sep 6, 2024 at 8:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.