0

Hey I'm currently having a hard time with image validation in my upload form.

The problem appears within my validation / submit function called when hitting the "Submit" Button. My Plan is to check the image dimensions. I'm doing it this way:

var file = $('.html5-wrapper .mandatoryImg')[i].files[0]
                        $scope.image = [];
                        var fileReader = new FileReader();
                        var image = new Image();
                        fileReader.onload = function (event) {
                            image.src = event.target.result;
                            image.onloadend = function() {
                                $scope.image.push(this.width)
                                $scope.image.push(this.height)
                                // update scope after image is updated
                                $scope.$apply();
                            };
                        };
                        fileReader.readAsDataURL(file);
                        var img = $scope.image
                        console.log(img);
                        console.log(img[0];)

The first log delivers the desired array. But when I try to access a single value in the second log, the value is undefined!

Can anyone help?

Pascal
  • 23
  • 8
  • Paste the json output of 1st log once. – Manish Singh Nov 11 '16 at 09:10
  • *"But when I try to access a single value in the second log, the value is undefined!"* Because you're accessing it on an empty array. Your final line runs **before** any of the content of the `onload` function runs. See the linked dupetarget for details. – T.J. Crowder Nov 11 '16 at 09:10
  • use `URL.createObjectURL(file)` instead... – Endless Nov 11 '16 at 09:32

0 Answers0