javascript检测图片大小、类型、长宽
来源:互联网 发布:站外优化 编辑:程序博客网 时间:2024/05/01 03:00
<!--
版本:20070421
功能:实现IE客户端检测图片大小,格式,文件名
参数:必填
作者:beimuaihui
代码格式化工具:SourceFormatX
调试工具:vs 2005
参考:通用表单检测代码(http://www.i170.com/Article/6845)
百度知道:http://zhidao.baidu.com
javascript参考手册:http://doc.51windows.net/jscript5/?url=/jscript5/dir.htm
谷歌:http://www.google.cn
-->
<!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>
<title>无标题页</title>
<script language="javascript" type="text/javascript">...
function checkForm()
...{
var centerlogo=document.getElementById("centerlogo");
var centerlogo2=document.getElementById("centerlogo2");
return checkphoto(centerlogo) && checkphoto(centerlogo2);
}
</script>
</head>
<body>
<form action="1.asp" onsubmit="return checkForm()">
<input name="centerlogo" id="centerlogo" type="file" require="false" accept=",jpg,jpeg,gif,png," picwidth="100"
picheight="115" picsize="30" onChange="checkphoto(this)">
<input name="centerlogo2" id="centerlogo2" type="file" require="false" accept=",jpg,jpeg,gif,png," picwidth="100"
picheight="115" picsize="30" onChange="checkphoto(this)">
<input type="submit" value="submit" />
</form>
<script language="javascript" type="text/javascript">...
// JavaScript Document
oFileChecker = new Image(); //定义一个图片,否则oFileChecker.onreadystatechange会报错
//图片装载时
oFileChecker.onreadystatechange = function ()
...{
//装载完成时
if (this.readyState == "complete")
...{
checkLength(this); //检测图片长宽
checkSize(this); //检测图片大小
if(errMsg!="")...{
alert(errMsg);
this.rule=false;
}
else
...{
this.rule=true;
}
}
}
//检测图片函数,并且返回是否正确
function checkphoto(photosrc)
...{
//初使化变量
oFileChecker.src="";
errMsg="";
oFileChecker.rule=false; //图片是否完成检测
//获取图片设置
w=parseInt(photosrc["picwidth"]); //图片的宽度
h=parseInt(photosrc["picheight"]); //图片的高度
limit=parseInt(photosrc["picsize"]); //图片的大小(KB)
allImgExt=photosrc["accept"]; //文件类型
required=photosrc["require"];//图片是否必填
filename=photosrc.value;//图片文件名
//如果图片不是必填项且为图片名为空
if(required=="false" && filename=="")
...{
return true;
}
checkType(); //检测扩展名
if(errMsg!="")
...{
alert(errMsg);
return false;
}
oFileChecker.src = photosrc.value;
window.setTimeout(function()...{},500);//延时一下,使大图片能完全载入(?)
return oFileChecker.rule;
}
//检测图片类型
function checkType()
...{
var fileExt=filename.substr(filename.lastIndexOf(".")+1).toLowerCase();
if(allImgExt.indexOf(","+fileExt+",")==-1)
...{
errMsg="图片类型应为"+allImgExt+"格式!";
}
}
//检测图片长宽
function checkLength(photo)
...{
//如果图片长宽没有设置或设置错误,还回true
if(isNaN(w) || isNaN(h))
return true;
if(photo.width!=w || photo.height!=h)
...{
errMsg="图片格式为"+w+"px*"+h+"px";
}
}
//检测图片文件大小
function checkSize(photo)
...{
//如果图片文件大小没有设置或设置错误,还回true
if(isNaN(limit))
return true;
if(photo.fileSize > limit*1024)
...{
errMsg="文件不能超过"+limit +"K";
}
}
</script>
</body>
</html>
版本:20070421
功能:实现IE客户端检测图片大小,格式,文件名
参数:必填
作者:beimuaihui
代码格式化工具:SourceFormatX
调试工具:vs 2005
参考:通用表单检测代码(http://www.i170.com/Article/6845)
百度知道:http://zhidao.baidu.com
javascript参考手册:http://doc.51windows.net/jscript5/?url=/jscript5/dir.htm
谷歌:http://www.google.cn
-->
<!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>
<title>无标题页</title>
<script language="javascript" type="text/javascript">...
function checkForm()
...{
var centerlogo=document.getElementById("centerlogo");
var centerlogo2=document.getElementById("centerlogo2");
return checkphoto(centerlogo) && checkphoto(centerlogo2);
}
</script>
</head>
<body>
<form action="1.asp" onsubmit="return checkForm()">
<input name="centerlogo" id="centerlogo" type="file" require="false" accept=",jpg,jpeg,gif,png," picwidth="100"
picheight="115" picsize="30" onChange="checkphoto(this)">
<input name="centerlogo2" id="centerlogo2" type="file" require="false" accept=",jpg,jpeg,gif,png," picwidth="100"
picheight="115" picsize="30" onChange="checkphoto(this)">
<input type="submit" value="submit" />
</form>
<script language="javascript" type="text/javascript">...
// JavaScript Document
oFileChecker = new Image(); //定义一个图片,否则oFileChecker.onreadystatechange会报错
//图片装载时
oFileChecker.onreadystatechange = function ()
...{
//装载完成时
if (this.readyState == "complete")
...{
checkLength(this); //检测图片长宽
checkSize(this); //检测图片大小
if(errMsg!="")...{
alert(errMsg);
this.rule=false;
}
else
...{
this.rule=true;
}
}
}
//检测图片函数,并且返回是否正确
function checkphoto(photosrc)
...{
//初使化变量
oFileChecker.src="";
errMsg="";
oFileChecker.rule=false; //图片是否完成检测
//获取图片设置
w=parseInt(photosrc["picwidth"]); //图片的宽度
h=parseInt(photosrc["picheight"]); //图片的高度
limit=parseInt(photosrc["picsize"]); //图片的大小(KB)
allImgExt=photosrc["accept"]; //文件类型
required=photosrc["require"];//图片是否必填
filename=photosrc.value;//图片文件名
//如果图片不是必填项且为图片名为空
if(required=="false" && filename=="")
...{
return true;
}
checkType(); //检测扩展名
if(errMsg!="")
...{
alert(errMsg);
return false;
}
oFileChecker.src = photosrc.value;
window.setTimeout(function()...{},500);//延时一下,使大图片能完全载入(?)
return oFileChecker.rule;
}
//检测图片类型
function checkType()
...{
var fileExt=filename.substr(filename.lastIndexOf(".")+1).toLowerCase();
if(allImgExt.indexOf(","+fileExt+",")==-1)
...{
errMsg="图片类型应为"+allImgExt+"格式!";
}
}
//检测图片长宽
function checkLength(photo)
...{
//如果图片长宽没有设置或设置错误,还回true
if(isNaN(w) || isNaN(h))
return true;
if(photo.width!=w || photo.height!=h)
...{
errMsg="图片格式为"+w+"px*"+h+"px";
}
}
//检测图片文件大小
function checkSize(photo)
...{
//如果图片文件大小没有设置或设置错误,还回true
if(isNaN(limit))
return true;
if(photo.fileSize > limit*1024)
...{
errMsg="文件不能超过"+limit +"K";
}
}
</script>
</body>
</html>
- javascript检测图片大小、类型、长宽
- struts2 上传 验证图片大小 长宽
- javascript 浏览器类型检测
- Javascript类型检测
- JavaScript类型的检测
- javascript 类型检测
- JavaScript中类型检测
- javascript 类型检测
- JavaScript类型检测
- JavaScript 的类型检测
- JavaScript-数据类型-类型检测
- JavaScript检测浏览器类型
- JavaScript类型检测详解
- JavaScript 类型检测
- javascript-深入浅出-类型检测
- javascript的类型检测
- JavaScript变量类型检测
- javascript类型检测
- Eclipse 主要插件搭配
- 理想中的项目经理
- 对csdn博客的建议
- jboss中ejb3“标识过长”问题。
- JAVASCRIPT精彩200例
- javascript检测图片大小、类型、长宽
- 第三十届ACM/ICPC 世界总决赛题目解析
- 宝元没前途,我的命就这样子吗
- Spring结合c3p0配置
- Java与UML交互图
- 图论算法
- 重拾LIUNX,有关用虚拟机上网(操作系统 RED HAT 9.0)
- (转)WTL开发者指南(WTL Developer’s Guide翻译)
- 调色板工作原理