图像宽度和高度未输入 jQuery

Image width and height is not coming in jQuery

我必须使用 jQuery 获取图像的宽度和高度,但我卡在这里了。 这里的图像宽度和高度显示未定义,请建议我。 感谢您的任何建议。

下面是代码

$(document).ready(function () {
  $("#file").change(function () {
      var files = this.files[0];
      var reader = new FileReader();
      var img = new Image();
      reader.onload = function (e) {
        img.src = e.target.result;
        var fileSize = Math.round(files.size / 1024);///output is coming.
        alert("File size is " + fileSize + " kb");
        alert("width=" + this.width + " height=" + this.height);//showing undefined
      };
      reader.readAsDataURL(files);
  });
});
<input type="file" class="cropit-image-input hii" id="file" style="display: none;" >

您可以使用 img 而不是 this

 alert("width=" + img.width + " height=" + img.height); 

$(document).ready(function() {
  $("#file").change(function() {
    var files = this.files[0];
    var reader = new FileReader();
    var img = new Image();
    reader.onload = function(e) {
      img.src = e.target.result;
      var fileSize = Math.round(files.size / 1024); ///output is coming.
      alert("File size is " + fileSize + " kb");
 
      alert("width=" + img.width + " height=" + img.height); //showing undefined

    };
    reader.readAsDataURL(files);

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" class="cropit-image-input hii" id="file">

我检查了我的桌面。

当我分别将this.width & this.height 改为img.width & img.height 时。成功了。

改变

alert("width=" + this.width + " height=" + this.height);//showing undefined

alert("width=" + img.width + " height=" + img.height);//showing undefined