Jquery图像和高度显示

anu*_*mol 5 jquery height image width

<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);
});
Run Code Online (Sandbox Code Playgroud)

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

Pra*_*lan 2

你可以做这样的事情

$('.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);
  }
});
Run Code Online (Sandbox Code Playgroud)
<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">
Run Code Online (Sandbox Code Playgroud)

参考:使用Javascript在上传之前检查图像的宽度和高度