ie8和chrome获取上传图片的宽度和高度等尺寸

时间:2022-05-06
本文章向大家介绍ie8和chrome获取上传图片的宽度和高度等尺寸,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
                  测试后可用

 <html>
 <head>
     <title>测试</title>
     <meta charset="utf-8"/>
     <link rel="stylesheet" href="http://g.tbcdn.cn/tb/global/2.9.1/global-min.css">
     <script src="http://g.tbcdn.cn/??kissy/k/1.4.2/seed-min.js"></script>
 </head>
 <body>


 <form action="" method="post">
     <input type="file" id="uploader" onchange="imgUpload()"/>
     <input type="submit" id="submit" onclick="test()"/>
 </form>
 <div id="tip1"></div>
 <div id="tip2"></div>
 <script type="text/javascript">
 var width;
 var height;
 function imgUpload() {
var obj=document.getElementById('uploader');
if(obj.files&&obj.files[0]){
var oFile=obj.files[0];
var reader=new FileReader();


reader.onload=function(){
// 也可以用 window.URL.createObjectURL(this.result)
var oImg=new Image();
oImg.src=this.result;
document.body.appendChild(oImg);

oImg.onload=function(){
width=oImg.offsetWidth;
height=oImg.offsetHeight;
//width=oImg.width;
//height=oImg.height;
document.body.removeChild(oImg);//放弃预览
};
};
reader.readAsDataURL(oFile);
}else{//ie 8
var img = new Image();
img.src = document.getElementById('uploader').value;
width=img.width;
height=img.height;

}
 }


 function test(){
alert(width);
alert(height); 
 }
 </script>
 </html>