【问题标题】:Jquery image and height displayjquery图像和高度显示
【发布时间】:2016-02-08 08:59:34
【问题描述】:
<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,但它没有显示正确的值。

【问题讨论】:

  • 为什么要使用img = new Image();创建新图像?
  • 不需要?
  • $('.test').bind('change', function() { var width = this.files[0].width; alert(width); });我已经改变了这样的代码。但它不工作..

标签: jquery image height width


【解决方案1】:

你可以这样做

$('.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

【讨论】:

  • @cd4xltech : 很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多