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 相关文章推荐
js 文本滚动效果的实例代码
Aug 17 Javascript
jquery全选checkBox功能实现代码(取消全选功能)
Dec 10 Javascript
jQuery 判断图片是否加载完成方法汇总
Aug 10 Javascript
15个常用的jquery代码片段
Dec 19 Javascript
浅谈js中对象的使用
Aug 11 Javascript
Vue使用vue-cli创建项目
Sep 01 Javascript
原生javascript实现文件异步上传的实例讲解
Oct 26 Javascript
基于javascript中的typeof和类型判断(详解)
Oct 27 Javascript
浅谈微信JS-SDK 微信分享接口开发(介绍版)
Aug 15 Javascript
vue--点击当前增加class,其他删除class的方法
Sep 15 Javascript
js实现移动端图片滑块验证功能
Sep 29 Javascript
vue 虚拟DOM的原理
Oct 03 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的FTP学习(一)[转自奥索]
2006/10/09 PHP
php操作MongoDB类实例
2015/06/17 PHP
php实现的双色球算法示例
2017/06/20 PHP
PHP数据库编程之MySQL优化策略概述
2017/08/16 PHP
实现复选框全选/全不选切换
2006/12/23 Javascript
javascript CSS画图之基础篇
2009/07/29 Javascript
JQuery 学习笔记01 JQuery初接触
2010/05/06 Javascript
JS获取浏览器语言动态加载JS文件示例代码
2014/10/31 Javascript
jQuery+ajax中getJSON() 用法实例
2014/12/22 Javascript
JavaScript获取当前运行脚本文件所在目录的方法
2016/02/03 Javascript
搞定immutable.js详细说明
2016/05/02 Javascript
EasyUI的doCellTip实现鼠标放到单元格上提示单元格内容
2016/08/24 Javascript
Bootstrap整体框架之JavaScript插件架构
2016/12/15 Javascript
jQuery中select与datalist制作下拉菜单时的区别浅析
2016/12/30 Javascript
input输入密码变黑点密文的实现方法
2017/01/09 Javascript
JavaScript上传文件时不用刷新页面方法总结(推荐)
2017/08/15 Javascript
使用canvas实现一个vue弹幕组件功能
2018/11/30 Javascript
用JS实现一个简单的打砖块游戏
2019/12/11 Javascript
JavaScript实现模态对话框实例
2020/01/13 Javascript
功能完善的小程序日历组件的实现
2020/03/31 Javascript
vue setInterval 定时器失效的解决方式
2020/07/30 Javascript
python发送邮件实例分享
2017/07/28 Python
python中不能连接超时的问题及解决方法
2018/06/10 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
Python Web框架之Django框架文件上传功能详解
2019/08/16 Python
CSS3制作轮播图的一种方法
2019/11/11 HTML / CSS
突破canvas语法限制 让他支持链式语法
2012/12/24 HTML / CSS
详解HTML5中download属性的应用
2015/08/06 HTML / CSS
毕业生幼师求职自荐信
2013/10/01 职场文书
高中生操行评语
2014/04/25 职场文书
护士节策划方案
2014/05/19 职场文书
大学生实习证明
2015/06/16 职场文书
单位接收证明格式
2015/06/18 职场文书
2015国庆66周年宣传语
2015/07/14 职场文书
《海上日出》教学反思
2016/02/23 职场文书
导游词之北京明十三陵
2019/10/28 职场文书