Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Open file dialog upon button click

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) { }

like image 425
Zvi Vered Avatar asked Oct 18 '25 15:10

Zvi Vered


2 Answers

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

like image 176
Abhinav Kumar Avatar answered Oct 22 '25 05:10

Abhinav Kumar


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>
like image 33
Alex Buchatski Avatar answered Oct 22 '25 05:10

Alex Buchatski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!