js检查上传图片大小和类型
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
照片:
<div id='phototd'>
<input type="file" name="photo" class="input" id="photo" onChange="UserUtil.initTestImg(this);setTimeout('UserUtil.checkPhoto()',100)" />
</div>
<hr>
<script language="javascript">
var $=function(id){return document.getElementById(id);}
var UserUtil={
photo_suffix : 'jpg|gif',
photo_size : 100,
photo_html : null,
test_img : new Image(),
img_size : 0,
init : function(){
this.photo_html=$('phototd').innerHTML;
this.test_img.onload=function(){
UserUtil.img_size=UserUtil.test_img.fileSize;
};
},
initTestImg : function(obj){
this.test_img.src=obj.value;
},
checkPassword : function(){
var password = $('password').value;
var repassword = $('rePassword').value;
var msg = repassword == password ? '' : '<font color="red">密码不一致,请确认!</font>';
$('passwordErrorMsg').innerHTML = msg;
return msg=='';
},
checkPhoto : function(){
var photo=$('photo');
if(!this.checkSuffix(photo.value)){
alert("对不起,您选择的图片类型不对,请选择以下图片类型:"+this.photo_suffix);
this.resetFile();
return false;
}
if(navigator.userAgent.indexOf('Firefox')>0)
this.img_size=photo.files.item(0).fileSize;
if(this.img_size/1024>this.photo_size){
alert("上传图片容量太大,选择的图片容量不能超过:" + (this.photo_size)+"K");
this.resetFile();
return false;
}
return true;
},
checkSuffix : function(path){
if(path.lastIndexOf('.')<0)
return false;
var fileName=path.substring(path.lastIndexOf('.')+1);
var suffixs=this.photo_suffix.split('|');
for(var i=0 ; i<suffixs.length ; i++){
if(fileName.toLowerCase()==suffixs[i].toLowerCase())
return true;
}
return false;
},
resetFile : function(){
$('phototd').innerHTML=this.photo_html;
},
save : function(){
if(Validator.Validate($('userForm'),3) && this.checkPassword() && checkPhoto())
$('userForm').submit();
}
};
UserUtil.init();
</script>
照片:
<div id='phototd'>
<input type="file" name="photo" class="input" id="photo" onChange="UserUtil.initTestImg(this);setTimeout('UserUtil.checkPhoto()',100)" />
</div>
<hr>
<script language="javascript">
var $=function(id){return document.getElementById(id);}
var UserUtil={
photo_suffix : 'jpg|gif',
photo_size : 100,
photo_html : null,
test_img : new Image(),
img_size : 0,
init : function(){
this.photo_html=$('phototd').innerHTML;
this.test_img.onload=function(){
UserUtil.img_size=UserUtil.test_img.fileSize;
};
},
initTestImg : function(obj){
this.test_img.src=obj.value;
},
checkPassword : function(){
var password = $('password').value;
var repassword = $('rePassword').value;
var msg = repassword == password ? '' : '<font color="red">密码不一致,请确认!</font>';
$('passwordErrorMsg').innerHTML = msg;
return msg=='';
},
checkPhoto : function(){
var photo=$('photo');
if(!this.checkSuffix(photo.value)){
alert("对不起,您选择的图片类型不对,请选择以下图片类型:"+this.photo_suffix);
this.resetFile();
return false;
}
if(navigator.userAgent.indexOf('Firefox')>0)
this.img_size=photo.files.item(0).fileSize;
if(this.img_size/1024>this.photo_size){
alert("上传图片容量太大,选择的图片容量不能超过:" + (this.photo_size)+"K");
this.resetFile();
return false;
}
return true;
},
checkSuffix : function(path){
if(path.lastIndexOf('.')<0)
return false;
var fileName=path.substring(path.lastIndexOf('.')+1);
var suffixs=this.photo_suffix.split('|');
for(var i=0 ; i<suffixs.length ; i++){
if(fileName.toLowerCase()==suffixs[i].toLowerCase())
return true;
}
return false;
},
resetFile : function(){
$('phototd').innerHTML=this.photo_html;
},
save : function(){
if(Validator.Validate($('userForm'),3) && this.checkPassword() && checkPhoto())
$('userForm').submit();
}
};
UserUtil.init();
</script>
sun