4

I'm using Angular 8. Upon button click I want to open a file dialog, select a file and then call an event that will handle this file.

Thank you, Zvika

I tried the following code in HTML. But I do not want anything to be displayed in HTML.

HTML

<p-fileUpload name="myfile[]" customUpload="true" auto="true" (uploadHandler)="onOpenFile($event)" accept="application/pdf"></p-fileUpload>

ts

public onOpenFile (event) { }

3 Answers 3

6

You can achieve this with normal input tag , you can make it hidden, but at least you need one element to trigger the click event. Check stackblitz

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to select the same file more than once? As for now handle(e) triggers only if we select a different file from one that was selected
You need to try something else, as change event has been bind on the file input tag. So it only trigger when it changes.
3

In addition to @Abhinav Kumar's answer regarding Primeng:

  <span pButton label="Import..." icon="pi pi-folder-open" (click)="fileInput.click()">
    <input #fileInput type="file" hidden (change)="doFileInput($event)">
  </span>

Comments

1

Posting only if it helps someone

In my case, I wanted to upload the same file twice. Other than that everything was working fine according to this example.

To upload the same file twice you need to clear the value of input after selecting a file. Like in the example below

Stackblitz example

1 Comment

Thank you very much !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.