Last active
July 18, 2017 09:28
-
-
Save phuwin1995/acbc9c949c6fc6084d96fa7654e127b0 to your computer and use it in GitHub Desktop.
Filesread for angular input. This supports multiple files adding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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