1

How to convert this to angular.js compitable so that i get the dataurl and send through $http.post

<input type="file" id="imgfiles" name="imgfiles" accept="image/jpeg" onchange="readURL(this);">


function readURL(input) {
    if (input.files[0].size <= 1048576) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#cam_photo').attr('src', e.target.result).width(250).height(230);
                var a = $('#cam_photo').attr('src');
                data_url = a;
            };
            reader.readAsDataURL(input.files[0]);
        }
    } else {
        alert('File is too large. Upload file less than 1MB');
    }
}
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
a45b
  • 489
  • 3
  • 19
  • for example see this answer: http://stackoverflow.com/questions/17063000/ng-model-for-input-type-file – michael Feb 11 '14 at 10:38

1 Answers1

0

Use angular.element(this).scope() to get the scope

Try this:

onchange="angular.element(this).scope().readURL(this)"

And then, write your $scope.readURL(input) code in your controller

$scope.readURL = function(input) {
    if (input.files[0].size <= 1048576) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#cam_photo').attr('src', e.target.result).width(250).height(230);
                var a = $('#cam_photo').attr('src');
                data_url = a;
            };
            reader.readAsDataURL(input.files[0]);
        }
    } else {
        alert('File is too large. Upload file less than 1MB');
    }
}
Community
  • 1
  • 1
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63