ie、火狐中js文件上传文件大小及格式检验

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script>

var uploadfile = {  issupport : false,  size : 0,  imgwidth : 0,  imgheight : 0,  flag : false }

String.prototype.endWith=function(str){  if(str==null||str==""||this.length==0||str.length>this.length)    return false;  if(this.substring(this.length-str.length)==str)    return true;  else    return false;  return true; }

function uploadfilesize(id){  var fobj = null;  if('string'==typeof(id)){   fobj = eval(id);  }else{   fobj = id;  }  if(fobj.files){//   uploadfile.size = fobj.files[0].fileSize;   if(fobj.files[0].fileSize==undefined){ //opera    uploadfile.size = fobj.files[0].size;    uploadfile.issupport = true;   }   uploadfile.issupport = true;  }else if(navigator.userAgent.indexOf("MSIE")>0){//IE   uploadfile.issupport = false;  }else{   uploadfile.issupport = false;  } }

function isuploadtypes(id, types){  var fobj = null;  if('string'==typeof(id)){   fobj = eval(id);  }else{   fobj = id;  }  var name = fobj.value;  if(types!=null && types.length>0){   for(var i=0; i<types.length; i++){    if(name.toLowerCase().endWith(types[i])){     return true;    }   }  }  return false; }

function isuploadimg(id){  var fobj = null;  if('string'==typeof(id)){   fobj = eval(id);  }else{   fobj = id;  }  var name = fobj.value;  if (name.toLowerCase().endWith(".jpg")   || name.toLowerCase().endWith(".jpeg")   || name.toLowerCase().endWith(".png")   || name.toLowerCase().endWith(".bmp")){   return true;  }  return false; } var image=new Image(); function uploadimg(id){  var fobj = null;  if('string'==typeof(id)){   fobj = eval(id);  }else{   fobj = id;  }    if(fobj.files){//   uploadfile.size = fobj.files[0].fileSize;   if(fobj.files[0].fileSize==undefined){ //opera    //image.src = fobj.value;    uploadfile.issupport = false;   }else   if(navigator.vendor.indexOf("Google")>=0){//google    //alert(file1.files[0].path);//还未找到办法    uploadfile.issupport = false;   }else{//火狐    uploadfile.issupport = true;    image.src = fobj.files.item(0).getAsDataURL();   }  }else if(navigator.userAgent.indexOf("MSIE")>0){//IE   uploadfile.issupport = true;   image.src = fobj.value;   uploadfile.size = image.fileSize;  }else{   uploadfile.issupport = false;;  }  if(uploadfile.issupport){   image.style.cssText="display:block;position:absolute; left: -5000px; top:-5000px;";   document.body.appendChild(image);   uploadfile.imgwidth = image.width;   uploadfile.imgheight = image.height;   if(uploadfile.imgwidth==0){    uploadfile.imgwidth = image.offsetWidth;    uploadfile.imgheight = image.offsetHeight;   }   if(fobj.files){    setTimeout("pausetime()",0.0000000000001);   }else{    setTimeout("pauseietime()",200);    image.onreadystatechange= function(){     if(image.readyState == "complete"){      uploadfile.size = image.fileSize;      uploadfile.imgwidth = image.offsetWidth;      uploadfile.imgheight = image.offsetHeight;      if(document.body.contains(image)){       document.body.removeChild(image);       uploadfile.flag = true;      }      //getHeight();     }    }   }   fobj = null;  } }

/** * 四舍五入函数 * @param {} Dight * @param {} How * @return {} */ function ForDight(Dight,How){  Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);  return Dight; }

function getFileSize(filesize){  var size = "未知";  var tmp = 0;  if(filesize!=0){   tmp = ForDight(filesize/1024,2);   if(tmp<1){    size="1KB";   }else if(tmp<1024&&tmp>1){    tmp = ForDight(filesize/1024,0);    size=tmp+"KB";   }else{    tmp = ForDight(tmp/1024,2);    if(tmp<1024){     size=tmp+"MB";    }else{     tmp = ForDight(tmp/1024,2);     size=tmp+"GB";    }   }  }  return size; }

function pausetime(){  uploadfile.imgwidth = image.offsetWidth;  uploadfile.imgheight = image.offsetHeight;  document.body.removeChild(image);  uploadfile.flag = true;  getHeight(); }

function pauseietime(){  if(document.body.contains(image)){   document.body.removeChild(image);   if(image.fileSize < 5 || (image.height==30 && image.width==28)){    uploadfile.flag = false;   }else{    uploadfile.flag = true;   }  }  getHeight(); }

function testsize(){  //if(isuploadimg('file1')){  if(isuploadtypes('file1',['.jpg','.gif','.png','.bmp','.txt'])){   uploadimg('file1');  }else{   alert('上传文件格式不对');  } }

function getHeight(){  if(uploadfile.flag){   alert("高度:"+uploadfile.imgheight+"宽度:"+uploadfile.imgwidth);   //alert("宽度"+uploadfile.imgwidth);   alert("大小:"+getFileSize(uploadfile.size));  }else{   alert('检测图片出错,请修改您的图片再上传');  } } </script> </head> <body>

<input type=file name="file1" id="file1" οnchange="testsize();"> <input type="button" value="test" οnclick="getHeight()"/>

</body> </html> http://www.w3help.org/zh-cn/causes/HF1010

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值