2015-11-07 69 views
5
<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但它没有显示正确的值。jquery图像和高度显示

+0

为什么您使用创造新形象'IMG =新的图片();'? –

+0

那是不需要的? 。 – anumol

+0

$( '试验 ')结合(' 改变',函数(){ 变种宽度= this.files [0] .WIDTH; 警报(宽度); }); 我改变了这样的代码。但它不工作.. – anumol

回答

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); 
 
    } 
 
});
<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

+1

非常感谢你的工作...... – anumol

+0

@ cd4xltech:很高兴帮助你:) –