vinz
vinz

Reputation: 348

Javascript - Image onload

I have a function in my js file to get the file height.

Util.isImageSize = function (file, callback) {
    var fileUpload = file;
    var reader = new FileReader();
    reader.readAsDataURL(fileUpload.files[0]);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var height = this.height;
            callback(height);
        };
    }
}

This is where I called the function

Util.isImageSize(document.getElementById('fileImage'), function(height) { 
    alert(height);
});

I am getting am error that says

Uncaught TypeError: callback is not a function at Image.image.onload 

I am really stuck at this right now

Upvotes: 2

Views: 1379

Answers (1)

Tan Duong
Tan Duong

Reputation: 2142

I'm not sure what are you looking for, but I still can alert image height

var Util = {};

Util.isImageSize = function (file, callback) {
    console.log(file);
    var fileUpload = file;
    var reader = new FileReader();
    reader.readAsDataURL(fileUpload.files[0]);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var height = this.height;
            callback(height);
        };
    }
}


document.getElementById('fileImage').addEventListener('change', function() {
    Util.isImageSize(this, function(height) { 
    alert(height);
});
})

http://jsfiddle.net/LvsYc/15487/

Upvotes: 4

Related Questions