2015-11-07 10 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); 
}); 

ich die Bildbreite und -höhe alarmieren wollte hochgeladen. Wie kann ich das machen ? Ich habe clientwidth und auch naturalwidth verwendet, aber es zeigt nicht den richtigen Wert an.JQuery Bild und Höhenanzeige

+0

warum erstellen Sie neues Bild 'img = new Bild() verwendet wird;'? –

+0

das wird nicht benötigt? . – anumol

+0

$ ('test ') bind (' change', function() { var width = this.files [0] .width; alert (Breite); }); Ich habe den Code so geändert. aber es funktioniert nicht .. – anumol

Antwort

2

Sie können so etwas wie dieses

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

Referenz tun: Check image width and height before upload with Javascript

+1

danke so sehr seine Arbeit ... – anumol

+0

@ cd4xltech: froh, dir zu helfen :) –