Is it possible to read files in AngularJS? I want to place the file into an HTML5 canvas to crop.
I was thinking of using a directive? This is the javscript code I want to put into my directive:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
I've taken Cherniv's code and folded all of it into a directive:
You can then use the directive as follows:
Where "file.data", "myLoaded", "myError", and "myProgress" are in the enclosing scope.
Yes, directives is a right way, but it looks little bit different:
Working example: http://plnkr.co/edit/y5n16v?p=preview
Thanks to lalalalalmbda for this link.
Angular File reader.
Live example : Live Run On Plunker