jquery图片及高度显示

标签 jquery image height 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);
});

我想提醒图像的宽度和高度已上传。我怎样才能做到这一点 ?我使用了 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

关于jquery图片及高度显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579915/

相关文章:

javascript - 在 jquery/javascript 中服务器端分页调用时,数据表需要成功函数

javascript - Dash.js - 从 MPEG-DASH 播放器捕获 404 错误

html - 如何让我的图像 float 到我的 div 的右侧?

height - 平均和/或最大视口(viewport)高度是多少?

jquery - 为什么在为 jQuery.ajax() 设置 post 数据时,serialize() 与 serializeArray() 具有相同的效果?

javascript - Qualtrics:jquery 附加的空按钮刷新页面

image - 有没有地方可以找到企业网站的图片

javascript - 在 Node.js 中如何将 multipart/form-data 上传的图像转换为 base64 格式

uiview - Xcode 8 为 UIview 返回 height=1000 和 Width =1000(实际大小为 (328,40))

flutter - MediaQuery.of(context).padding.top 返回 0.0