首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JS得到图片的尺寸解决方案

2012-02-05 
JS得到图片的尺寸页面上我定义了标签,用来上传图片s:file nameaa idaa ContentEditablefalse si

JS得到图片的尺寸
页面上我定义了标签,用来上传图片
<s:file name="aa" id="aa" ContentEditable="false" size="45"></s:file> 
提交时我要得到上传图片的大小和尺寸,我用下面的JS来做:
function checkImage(file){
var fso,f;  
fso=new ActiveXObject("Scripting.FileSystemObject");  
f=fso.GetFile(file);
var fileSize = f.size ;
if((fileSize/1024) > 2){
alert("上传图片文件不能大于2K");
return false;
}
var imgSize={  
  width:0,  
  height:0  
  };  
  image=new Image();  
  image.src=file;  
  imgSize.width =image .width;  
  imgSize .height=image .height;  
  alert( imgSize.width);
return true;
}

但这样我始终得不到图片的尺寸大小,一直提示0

[解决办法]

Java code
var flag=false; function sImage(ImgS){ var image=new Image(); image.src=ImgS.src; if(image.width>0 && image.height>0){ flag=true; if(image.width/image.height>= 173/173){ if(image.width>173){ ImgS.width=173; ImgS.height=(image.height*173)/image.width; }else{ ImgS.width=image.width; ImgS.height=image.height; } //ImgS.alt="实际图片大小为"+image.width+"×"+image.height; } else{ if(image.height>173){ ImgS.height=173; ImgS.width=(image.width*173)/image.height; }else{ ImgS.width=image.width; ImgS.height=image.height; } //ImgS.alt=image.width+"×"+image.height; } } }
[解决办法]
HTML code
<input type="file" id="fileText">    <input type="button" value="getSize" onclick="checkFileChange(document.getElementById('fileText'));">       <script type="text/javascript">   var  Sys = {};   if(navigator.userAgent.indexOf("MSIE")>0)   {       Sys.ie=true;   }   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)   {       Sys.firefox=true;   }     function checkFileChange(obj)   {       var filesize = 0;              if(Sys.firefox)       {           filesize = obj.files[0].fileSize;       }else if(Sys.ie)       {           var fileobject = new ActiveXObject ("Scripting.FileSystemObject");        var file = fileobject.GetFile (document.getElementById("fileText").value);        var filesize = file.Size;    }       alert(filesize);   }   </script> 

热点排行