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 相关文章推荐
使用滤镜设置透明导致 IE 6/7/8/9 解析异常的解决方法
Apr 07 Javascript
JavaScript实现随机替换图片的方法
Apr 16 Javascript
javascript实现汉字转拼音代码分享
Apr 20 Javascript
javascript实现抽奖程序的简单实例
Jun 07 Javascript
Augularjs-起步详解
Jul 08 Javascript
浅谈vue-router 路由传参的方法
Dec 27 Javascript
使用live-server快速搭建本地服务器+自动刷新的方法
Mar 09 Javascript
JavaScript中为事件指定处理程序的五种方式分析
Jul 27 Javascript
小程序实现录音上传功能
Nov 22 Javascript
使用Bootstrap做一个朝代历史表
Dec 10 Javascript
js实现消灭星星(web简易版)
Mar 24 Javascript
antd的select下拉框因为数据量太大造成卡顿的解决方式
Oct 31 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
PHP6 先修班 JSON实例代码
2008/08/23 PHP
Twig模板引擎用法入门教程
2016/01/20 PHP
详解php中生成标准uuid(guid)的方法
2019/04/28 PHP
List the Codec Files on a Computer
2007/06/11 Javascript
A标签中通过href和onclick传递的this对象实现思路
2013/04/19 Javascript
jquery购物车实时结算特效实现思路
2013/09/23 Javascript
Lab.js初次使用笔记
2015/02/28 Javascript
Vue.js常用指令汇总(v-if、v-for等)
2016/11/03 Javascript
详解nodejs微信公众号开发——4.自动回复各种消息
2017/04/11 NodeJs
原生js实现简单的链式操作
2017/07/04 Javascript
nodejs 搭建简易服务器的图文教程(推荐)
2017/07/18 NodeJs
简单快速的实现js计算器功能
2017/08/17 Javascript
JavaScript常见JSON操作实例分析
2018/08/08 Javascript
jQuery实现鼠标滑动切换图片
2020/05/27 jQuery
python通过pil将图片转换成黑白效果的方法
2015/03/16 Python
Python编程深度学习计算库之numpy
2018/12/28 Python
python二进制读写及特殊码同步实现详解
2019/10/11 Python
python3反转字符串的3种方法(小结)
2019/11/07 Python
浅谈Pycharm的项目文件名是红色的原因及解决方式
2020/06/01 Python
python实现批量转换图片为黑白
2020/06/16 Python
Python如何将字符串转换为日期
2020/07/31 Python
香港时装购物网站:ZALORA香港
2017/04/23 全球购物
澳大利亚自然和有机的健康美容产品一站式商店:Ziani Beauty
2017/12/28 全球购物
德国综合购物网站:OTTO
2018/11/13 全球购物
面试后感谢信怎么写
2014/02/01 职场文书
幼儿园中秋节活动反思
2014/02/16 职场文书
情人节寄语大全
2014/04/11 职场文书
国贸专业毕业求职信
2014/06/11 职场文书
品质口号大全
2014/06/17 职场文书
自愿离婚协议书2015
2015/01/26 职场文书
长城英文导游词
2015/01/30 职场文书
储备店长岗位职责
2015/04/14 职场文书
职位证明模板
2015/06/23 职场文书
重阳节座谈会主持词
2015/07/03 职场文书
运动会主持人开幕词
2016/03/04 职场文书
Springboot/Springcloud项目集成redis进行存取的过程解析
2021/12/04 Redis