Skip to content

Instantly share code, notes, and snippets.

@phuwin1995
Last active July 18, 2017 09:28
Show Gist options
  • Save phuwin1995/acbc9c949c6fc6084d96fa7654e127b0 to your computer and use it in GitHub Desktop.
Save phuwin1995/acbc9c949c6fc6084d96fa7654e127b0 to your computer and use it in GitHub Desktop.
Filesread for angular input. This supports multiple files adding.
app.directive("filesread", [function () {
return {
scope: {
filesread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var files = changeEvent.target.files;
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
reader.onload = function(loadEvent){
var name = this.fileName;
var type = this.type;
var file = this.file;
scope.$apply(function(){
if (scope.filesread == undefined) scope.filesread = []
scope.filesread.push({
"url":loadEvent.target.result,
"name":name,
"type":type,
"file":file,
});
})
}
reader.file = files[i];
reader.fileName = files[i].name;
reader.type = files[i].type;
reader.readAsDataURL(files[i]);
}
});
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment