Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 94669  / 11 Years ago, mon, september 16, 2013, 12:00:00

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 javascript 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]);
}
}

More From » angularjs

 Answers
12

Yes, directives is a right way, but it looks little bit different:



.directive(ngFileSelect,function(){    
return {
link: function($scope,el){
el.bind(change, function(e){
$scope.file = (e.srcElement || e.target).files[0];
$scope.getFile();
});
}
}
})


Working example: http://plnkr.co/edit/y5n16v?p=preview



Thanks to lalalalalmbda for this link.


[#75658] Saturday, September 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaylea

Total Points: 459
Total Questions: 97
Total Answers: 106

Location: Denmark
Member since Wed, Aug 26, 2020
4 Years ago
;