0

i am trying this code but get only image size not image width.

$('#myFile').bind('change', function() {

    var size = this.files[0].size;
    var w = this.files[0].width;
    var sizeKB = Math.round(size/1024);
    alert(sizeKB+'KB');
    alert(w);

});

<input type="file" id="myFile" />

anybody have solution?

anosim
  • 73
  • 3
  • 12

4 Answers4

1

write this code on image change event

         var _URL = window.URL;
        function checkImage(fileObj){
        var file, img;
        if ((file = fileObj.files[0])) {
            img = new Image();
            img.onload = function () {
                alert("Width:" + this.width + "   Height: " + this.height);//this will give you image width and height and you can easily validate here....
            };
            img.src = _URL.createObjectURL(file);
        }
    }

this will give height and width both on load of image

Affan
  • 1,132
  • 7
  • 16
0

with jQuery:

$(this).prop('files')[0].size
pekcheng
  • 354
  • 1
  • 5
  • 14
0

Make sure that the browser you are testing this code under supports the HTML5 File API. Here's a nice article you may take a look at.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

You cannot access an image using raw JavaScript. You will need an alternate by using a flash uploader of some kind like Uploadify.

MacMac
  • 34,294
  • 55
  • 151
  • 222