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'
Solution 1:
Nice solution by leocaseiro
<input class="ng-hide" id="input-file-id" multiple type="file" />
<labelfor="input-file-id"class="md-button md-raised md-primary">Choose Files</label>
View in codepen
Solution 2:
For Angular 6+:
HTML:
<input #csvInputhidden="true"type="file"onclick="this.value=null" (change)="csvInputChange($event)"accept=".csv"/><buttonmat-flat-buttoncolor="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.
Solution 3:
Another example of the solution. Will look like the following
CodePen link there.
<choose-file layout="row">
<input id="fileInput"type="file" class="ng-hide">
<md-input-container flex class="md-block">
<inputtype="text" ng-model="fileName" disabled>
<div class="hint">Select your file</div>
</md-input-container>
<div>
<md-button id="uploadButton" class="md-fab md-mini">
<md-icon class="material-icons">attach_file</md-icon>
</md-button>
</div>
</choose-file>
.directive('chooseFile', function() {
return {
link: function(scope, elem, attrs) {
var button = elem.find('button');
var input = angular.element(elem[0].querySelector('input#fileInput'));
button.bind('click', function() {
input[0].click();
});
input.bind('change', function(e) {
scope.$apply(function() {
var files = e.target.files;
if (files[0]) {
scope.fileName = files[0].name;
} else {
scope.fileName = null;
}
});
});
}
};
});
Hope it helps!
Solution 4:
Based on this answer. It took some time for me to make this approach working, so I hope my answer will save someone's time.
Directive:
angular.module('app').directive('apsUploadFile', apsUploadFile);
functionapsUploadFile() {
var directive = {
restrict: 'E',
templateUrl: 'upload.file.template.html',
link: apsUploadFileLink
};
return directive;
}
functionapsUploadFileLink(scope, element, attrs) {
var input = $(element[0].querySelector('#fileInput'));
var button = $(element[0].querySelector('#uploadButton'));
var textInput = $(element[0].querySelector('#textInput'));
if (input.length && button.length && textInput.length) {
button.click(function (e) {
input.click();
});
textInput.click(function (e) {
input.click();
});
}
input.on('change', function (e) {
var files = e.target.files;
if (files[0]) {
scope.fileName = files[0].name;
} else {
scope.fileName = null;
}
scope.$apply();
});
}
upload.file.template.html
<input id="fileInput"type="file" class="ng-hide">
<md-button id="uploadButton"
class="md-raised md-primary"
aria-label="attach_file">
Choose file
</md-button>
<md-input-container md-no-float>
<input id="textInput" ng-model="fileName"type="text" placeholder="No file chosen" ng-readonly="true">
</md-input-container>
Solution 5:
from jameswyse at https://github.com/angular/material/issues/3310
HTML
<input id="fileInput" name="file"type="file" class="ng-hide" multiple>
<md-button id="uploadButton" class="md-raised md-primary"> Choose Files </md-button>
CONTROLLER
var link = function (scope, element, attrs) {
const input = element.find('#fileInput');
const button = element.find('#uploadButton');
if (input.length && button.length) {
button.click((e) => input.click());
}
}
Worked for me.
Post a Comment for "File Upload With Angular Material"