user1424508 user1424508 - 7 months ago 26
AngularJS Question

How to read a file in AngularJS?

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) {



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

  return {
    link: function($scope,el){          
      el.bind("change", function(e){          
        $scope.file = (e.srcElement ||[0];

Working example:

Thanks to lalalalalmbda for this link.