js校验图片大小尺寸

<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>标题文档</title>
<script>
UpLoadFileCheck=function()
{  
    this.AllowExt=".jpg,.gif";//允许传文件类型 0无限制 每扩展名边要加"," 写字母表示 
    this.AllowImgFileSize=0;//允许传文件大小 0无限制 单位:KB 
    this.AllowImgWidth=0;//允许传图片宽度 0无限制 单位:px(像素) 
    this.AllowImgHeight=0;//允许传图片高度 0无限制 单位:px(像素) 
    this.ImgObj=new Image();
    this.ImgFileSize=0;
    this.ImgWidth=0;
    this.ImgHeight=0;
    this.FileExt="";
    this.ErrMsg="";
    this.IsImg=false;//全局变量
    
}

UpLoadFileCheck.prototype.CheckExt=function(obj)
{
   this.ErrMsg=""; 
   this.ImgObj.src=obj.value; 
   //this.HasChecked=false; 
   if(obj.value=="")
   {
    this.ErrMsg="\n请选择文件";    
   }
   else
   {   
    this.FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase(); 
    if(this.AllowExt!=0&&this.AllowExt.indexOf(this.FileExt)==-1)//判断文件类型否允许传 
    { 
     this.ErrMsg="\n该文件类型允许传请传 "+this.AllowExt+" 类型文件前文件类型"+this.FileExt;  
    }
   } 
   if(this.ErrMsg!="") 
   {
    this.ShowMsg(this.ErrMsg,false); 
    return false;
   }
   else   
    return this.CheckProperty(obj);    
}

UpLoadFileCheck.prototype.CheckProperty=function(obj)
{
   if(this.ImgObj.readyState!="complete")//
    { 
     sleep(1000);//秒使用图能完全加载    
    }   
   
   if(this.IsImg==true)
   {
    this.ImgWidth=this.ImgObj.width;//取图片宽度 
    this.ImgHeight=this.ImgObj.height;//取图片高度
    if(this.AllowImgWidth!=0&&this.AllowImgWidth<this.ImgWidth) 
    this.ErrMsg=this.ErrMsg+"\n图片宽度超限制请传宽度于"+this.AllowImgWidth+"px文件前图片宽度"+this.ImgWidth+"px"; 

    if(this.AllowImgHeight!=0&&this.AllowImgHeight<this.ImgHeight) 
    this.ErrMsg=this.ErrMsg+"\n图片高度超限制请传高度于"+this.AllowImgHeight+"px文件前图片高度"+this.ImgHeight+"px"; 
   }

   this.ImgFileSize=Math.round(this.ImgObj.fileSize/1024*100)/100;//取图片文件 
   if(this.AllowImgFileSize!=0&&this.AllowImgFileSize<this.ImgFileSize) 
    this.ErrMsg=this.ErrMsg+"\n文件超限制请传于"+this.AllowImgFileSize+"KB文件前文件"+this.ImgFileSize+"KB"; 

   if(this.ErrMsg!="") 
   {
    this.ShowMsg(this.ErrMsg,false); 
    return false;
   }
   else 
    return true; 
} 

UpLoadFileCheck.prototype.ShowMsg=function(msg,tf)//显示提示信息 tf=false 显示错误信息 msg-信息内容 
{ 
   alert(msg);
}
function sleep(num)   
   {   
    var   tempDate=new Date();   
    var   tempStr="";   
    var   theXmlHttp= new ActiveXObject("Microsoft.XMLHTTP");   
    while((new Date()-tempDate)<num)   
    {   
    tempStr+="\n"+(new Date()-tempDate);   
    try{   
    theXmlHttp.open("get","about:blank?JK="+Math.random(), false);   
    theXmlHttp.send();   
    }   
    catch(e){;}   
    }   
   //containerDiv.innerText=tempStr;   
   return;   
   }  
function c(obj)
{
   var d=new UpLoadFileCheck(); 
   d.IsImg=true;
   d.AllowImgFileSize=160;
   d.AllowImgWidth=10;
   d.AllowImgHeight=10;
   d.CheckExt(obj);
}
</script>
</head>
<body>
<input name="imagePhoto" type="file" οnchange="c(this)"/>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值