不改安全级别,上传前,在客户端判断图片文件大小

很早前收集的TIPS,最近在项目中用到了,可能会对大家有所帮助。

 1<script>   
 2var img=null;   
 3function s()   
 4{   
 5if(img)img.removeNode(true);   
 6img=document.createElement("img");   
 7img.style.position="absolute";   
 8img.style.visibility="hidden";   
 9img.width = 0;   
10img.height = 0;   
11img.attachEvent("onreadystatechange",orsc);   
12img.attachEvent("onerror",oe);   
13document.body.  insertAdjacentElement  ("beforeend",img);   
14img.src=inp.value;   
15}   
16function oe()   
17{   
18alert("cant load img");   
19}   
20function orsc()   
21{   
22if(img.readyState!="complete")return false;   
23alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);   
24alert("图片尺寸:"+img.fileSize);   
25btn.disabled=false;   
26}   
27</script>
1<input id="inp" type="file"/>
1<br/>
1<button onclick="s()">Test</button>
1<button disabled="" id="btn">UpLoad</button>

大家看到,其实也很简单,它先是在内存中定义了一个IMG对象,然后用此对象在客户端得到IMG对象的各种属性。

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus