日期:2013-11-04  浏览次数:20435 次


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





<!--摘自51js-->

<script>
var img=null;
function s()
{
if(img)img.removeNode(true);
img=document.createElement("img");
img.style.position="absolute";
img.style.visibility="hidden";
img.width = 0;
img.height = 0;
img.attachEvent("onreadystatechange",orsc);
img.attachEvent("onerror",oe);
document.body.insertAdjacentElement("beforeend",img);
img.src=inp.value;
}
function oe()
{
alert("cant load img");
}
function orsc()
{
if(img.readyState!="complete")return false;
alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);
alert("图片尺寸:"+img.fileSize);
btn.disabled=false;
}
</script>
<input id=inp type="file">
<br>
<button >Test</button><button id=btn disabled>UpLoad</button>
 
大家看到,其实也很简单,它先是在内存中定义了一个IMG对象,然后用此对象在客户端得到IMG对象的各种属性。