user1424508 user1424508 - 1 year ago 93
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) {


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download