ie、火狐中js文件上传文件大小及格式检验
2011-09-28 19:16
561 查看
<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" onchange="testsize();"> <input type="button" value="test" onclick="getHeight()"/> </body> </html> http://www.w3help.org/zh-cn/causes/HF1010
相关文章推荐
- Js下检查上传图片文件格式和大小的方法,兼容ie和火狐
- js兼容ie和chrome的文件上传大小校验
- js判断上传文件的大小,及确认文件是否在本地存在,兼容firefox、chrome、ie等浏览器
- IE+JS: 上传之前检测图片文件大小
- input file 控制上传文件的大小和格式(兼容ie)
- 多文件上传,使用js对文件进行校验,包括文件单个大小,格式,总的文件大小,文件是否为空等
- js 判断上传文件大小及格式代码
- 多个上传文件用js验证文件的格式和大小的方法(推荐)
- JS获取IE上传文件路径、后缀、大小、文件名代码
- JS判断上传文件是否符合格式和大小要求
- js获取上传文件大小,兼容ie,FireFox,Chrome
- IE+JS: 上传之前检测图片文件大小及类型
- js判断上传文件的格式和大小
- js实现上传文件大小限制,兼容IE、Firefox、Chrome
- ajaxFileUpload.js上传图片插件,全浏览器兼容,规避json错误,带文件格式大小拦截
- 客户端js校验文件上传格式与文件大小
- js判断上传文件的格式和大小
- js显示上传图片的缩略图、验证上传文件的格式、验证上传文件大小、验证图片宽、高
- js提交form表单 - input file 文件上传控制上传文件的大小和格式
- js提交form表单 - input file 文件上传控制上传文件的大小和格式