Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload with Angular Material

I'm writing an web app with AngularJS and angular-material. The problem is that there's no built-in component for file input in angular-material. (I feel that file uploading doesn't fit the material design, but I need it in my app)

Do you have a good solution for this problem?

like image 761
WreckingBall Avatar asked Aug 06 '15 23:08

WreckingBall


People also ask

How do you create an HTML file to upload?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

Nice solution by leocaseiro

<input class="ng-hide" id="input-file-id" multiple type="file" /> <label for="input-file-id" class="md-button md-raised md-primary">Choose Files</label> 

enter image description here

View in codepen

like image 146
Radin Reth Avatar answered Sep 25 '22 12:09

Radin Reth


For Angular 6+:

HTML:

<input #csvInput hidden="true" type="file" onclick="this.value=null" (change)="csvInputChange($event)" accept=".csv"/> <button mat-flat-button color="primary" (click)="csvInput.click()">Choose Spreadsheet File (CSV)</button> 

Component method:

  csvInputChange(fileInputEvent: any) {     console.log(fileInputEvent.target.files[0]);   } 

Note: This filters to just allow .csv files.

like image 45
rynop Avatar answered Sep 26 '22 12:09

rynop