0

How can I take value of w and h out side the below code. I need to get the width and height of each image that is selected to upload.

     var widt;
            var hit;
            reader.readAsDataURL(file);
            reader.onload = function (_file) {
                image.src = _file.target.result;              // url.createObjectURL(file);
                image.onload = function () {
                    var w = this.width,
                        h = this.height,
                        t = file.type,                           // ext only: // file.type.split('/')[1],
                        n = file.name
                    widt = w;
                    hit = h;
                };
            };
console.log(widt);

Here console.log returns undefined.

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
Neethu
  • 85
  • 4
  • 9

3 Answers3

0

Your variable name is widt and your are using width in the console log. Try:

console.log(widt);

Also, put that console log inside the image.onload function to get the correct response.

palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

You missed the semi-colon at the end of following line of code

  n = file.name
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
0
  1. "console.log(widh);" change to "console.log(widt);"
  2. put "console.log(widt);" into "image.onload" function.

code:

image.onload = function () {
  var w = this.width,
      h = this.height,
      t = file.type,                           
      n = file.name;
  widt = w;
  hit = h;
  console.log(widt);
};
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46