js验证上传图片的方法


Posted in Javascript onMay 12, 2015

本文实例讲述了js验证上传图片的方法。分享给大家供大家参考。具体实现方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js验证图片</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-信息内容 
 { 
 /*msg=msg.replace("\n","<li>"); 
 msg=msg.replace(/\n/gi,"<li>"); 
  */
 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=100;
 d.CheckExt(obj)
 }
</script>
</head>
<body>
<input name="" type="file" onchange="c(this)"/>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
jquery操作cookie插件分享
Jan 14 Javascript
Jquery Ajax xmlhttp请求成功问题
Feb 04 Javascript
JQuery中attr方法和removeAttr方法用法实例
May 18 Javascript
js 实现数值的千分位及保存小数方法(推荐)
Aug 01 Javascript
javascript数组常用方法汇总
Sep 10 Javascript
详解获取jq ul第一个li定位的四种解决方案
Nov 23 Javascript
JavaScript实现256色转灰度图
Feb 22 Javascript
微信小程序scroll-view实现横向滚动和上拉加载示例
Mar 06 Javascript
jQuery插件zTree实现获取当前选中节点在同级节点中序号的方法
Mar 08 Javascript
vue2.x 父组件监听子组件事件并传回信息的方法
Jul 17 Javascript
vue实现提示保存后退出的方法
Mar 15 Javascript
vue的滚动条插件实现代码
Sep 07 Javascript
js中setTimeout()与clearTimeout()用法实例浅析
May 12 #Javascript
js实现一个链接打开两个链接地址的方法
May 12 #Javascript
js实现鼠标经过表格行变色的方法
May 12 #Javascript
js比较日期大小的方法
May 12 #Javascript
js实现简单div拖拽功能实例
May 12 #Javascript
js实现两点之间画线的方法
May 12 #Javascript
jquery简单实现图片切换效果的方法
May 12 #Javascript
You might like
PHP 7.1新特性的汇总介绍
2016/12/16 PHP
JavaScript脚本性能的优化方法
2007/02/02 Javascript
最新28个很棒的jQuery 教程
2011/05/28 Javascript
javascript的字符串按引用复制和传递,按值来比较介绍与应用
2012/12/28 Javascript
JS 两个字符串时间的天数差计算
2013/08/25 Javascript
JS滚轮事件onmousewheel使用介绍
2013/11/01 Javascript
表单提交前触发函数返回true表单才会提交
2014/03/11 Javascript
JS实现点击按钮后框架内载入不同网页的方法
2015/05/05 Javascript
超漂亮的jQuery图片轮播特效
2015/11/24 Javascript
jquery ztree实现树的搜索功能
2016/02/25 Javascript
浅谈javascript中new操作符的原理
2016/06/07 Javascript
jquery pagination分页插件使用详解(后台struts2)
2017/01/22 Javascript
ES6新特性四:变量的解构赋值实例
2017/04/21 Javascript
jQuery+PHP+Mysql实现抽奖程序
2020/04/12 jQuery
基于vue2.0动态组件及render详解
2018/03/17 Javascript
使用Vue如何写一个双向数据绑定(面试常见)
2018/04/20 Javascript
深入浅析Node.js 事件循环、定时器和process.nextTick()
2018/10/22 Javascript
vue-cli 3 全局过滤器的实例代码详解
2019/06/03 Javascript
[02:27]《DAC最前线》之附加赛征程
2015/01/29 DOTA
[49:05]OG vs Newbee 2019DOTA2国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21.mp4
2020/07/19 DOTA
Python常见文件操作的函数示例代码
2011/11/15 Python
python实现探测socket和web服务示例
2014/03/28 Python
python嵌套字典比较值与取值的实现示例
2017/11/03 Python
浅析python实现scrapy定时执行爬虫
2018/03/04 Python
python中将zip压缩包转为gz.tar的方法
2018/10/18 Python
Python调用服务接口的实例
2019/01/03 Python
Python3的socket使用方法详解
2020/02/18 Python
清除canvas画布内容(点擦除+线擦除)
2020/08/12 HTML / CSS
荷兰电脑专场:Paradigit
2018/05/05 全球购物
Footshop乌克兰:运动鞋的最大选择
2019/12/01 全球购物
大学毕业生通用自荐信范文
2013/10/31 职场文书
函授毕业自我鉴定
2014/02/04 职场文书
工作迟到检讨书
2014/02/21 职场文书
教师先进事迹材料
2014/12/16 职场文书
给老师的保证书怎么写
2015/05/09 职场文书
zabbix 代理服务器的部署与 zabbix-snmp 监控问题
2022/07/15 Servers