专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Web开发

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

发布时间:2010-05-31 07:02:38 文章来源:www.iduyao.cn 采编人员:星星草
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>
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: