Jquery 图片和高度显示

Jquery image and height display

<input type="file" class="test" name="vend_logo" id="vend_logo">

$('.test').bind('change', function() {
var file = files[0]
var img = new Image();
var width = img.width;alert(width);
});

我想提醒图片的宽度和高度已上传。我怎样才能做到这一点 ?我使用了 clientwidthnaturalwidth 但它没有显示正确的值。

你可以这样做

$('.test').bind('change', function() {
  if (file = this.files[0]) {
    img = new Image();
    img.onload = function() {
      var width = this.width;
      alert(width);
    };
    img.src = window.URL.createObjectURL(file);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="file" class="test" name="vend_logo" id="vend_logo">

参考:Check image width and height before upload with Javascript