admin管理员组文章数量:1405576
I am trying to validate the image uploaded by the user. So, after the user adds its image, before uploading it on the server, I tried validating the image dimensions with the javascript code below but it won't work.
function check_image_dimensions(){
var t=document.getElementById("main_file_1");
e=t.clientWidth;
n="";
o="We could not upload your image because \n \n";
if (e<300) {
(n+="The height of the image must be greater than 300px \n";
}
if(i<300){
n+="The height of the image must be greater than 300px \n";
}
I also tried:
e=t.width;
i=t.height;
But it still doesn't work. What am I doing wrong?
Thank you!
I am trying to validate the image uploaded by the user. So, after the user adds its image, before uploading it on the server, I tried validating the image dimensions with the javascript code below but it won't work.
function check_image_dimensions(){
var t=document.getElementById("main_file_1");
e=t.clientWidth;
n="";
o="We could not upload your image because \n \n";
if (e<300) {
(n+="The height of the image must be greater than 300px \n";
}
if(i<300){
n+="The height of the image must be greater than 300px \n";
}
I also tried:
e=t.width;
i=t.height;
But it still doesn't work. What am I doing wrong?
Thank you!
Share Improve this question asked Nov 6, 2013 at 23:10 user2961763user2961763 631 silver badge9 bronze badges 2- 1 What is main_file_1, an img tag, or the file input tag? – varfoo Commented Nov 6, 2013 at 23:16
- It's the id I gave to the image. I have: <input type="file" id="main_file_1" name="main_file_1"> – user2961763 Commented Nov 6, 2013 at 23:23
1 Answer
Reset to default 8Something like:
1) Put the image in a hidden <img/>
element
See this example: MDN - Using files from web applications
2) Check the size (bounding box) of the <img/>
element
if(img.getBoundingClientRect().width<300) ...
3) Alert the user (or not)
Here is a working version: JSFiddle.
Browser Compatability: Firefox 3.6+, Chrome 7+, IE 10+, Opera 12+
本文标签: javascriptPicture height and width before uploadingStack Overflow
版权声明:本文标题:javascript - Picture height and width before uploading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744325686a2600714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论