首页 > 网站 > WEB开发 > 正文

Javascript 验证上传图片大小[客户端验证]

2024-04-27 14:21:02
字体:
来源:转载
供稿:网友

javascript 验证上传图片大小[客户端验证]

需求分析:在做上传图片的时候,如果不限制上传图片大小,后果非常的严重。那么我们怎样才可以解决一个棘手的问题呢?有两种方式:1)后台处理: 也就是Ajax POST提交到后台,把图片上传到服务器上,然后获得该图片大小做处理。2)前台处理: 也就是利用Javascript获取该图片大小。显然第一种方式,很不好。因为需要把文件先上传到服务器上,如果文件很大的话,在加上网不是很快,需要等待好长时间,治标不治本。功能解析:在这里我只介绍IE与Firefox两个浏览器的不同做法。IE6:关键字: fileSize onreadystatechange complete在IE6中可以通过Img对象的fileSize 属性获得文件大小,但这个fileSize属性的正确值是建立在onreadystatechange 事件的complete 中,也就是

Java代码收藏代码
  1. <imgsrc=""class="img"
  2. onreadystatechange="Javascript:sizeCheck(this);">
  3. functionsizeCheck(img){
  4. if(img.readyState=="complete"){
  5. alert(img.fileSize);
  6. }
  7. }

FireFox3.0:关键字: getAsDataURL() fileSize在FireFox中处于安全的考虑,无法获得上传图片的完整路径,只能获得图片名称。但浏览器提供nsIDOMFile這樣一個接口,所以需要通过getAsDataURL()获得处理过后的路径,但该路径不影响图片src显示。nsIDOMFile接口:DOMString getAsBinary();DOMString getAsDataURL();DOMString getAsText(in DOMString encoding);

Java代码收藏代码
  1. <inputtype="file"name="uploadImg"
  2. onchange="Javascript:checkFileChange(this);"
  3. size="12"/>
  4. functioncheckFileChange(obj){
  5. varimg=document.getElementById("img");
  6. img.src=obj.files[0].getAsDataUrl();
  7. alert(obj.files[0].fileSize);
  8. }

以上是两个不同浏览器的处理方式,那么怎么把他们融和到一起呢?我在下面会将我做的小例子贴出来,其中里面我使用JQuery,方便与获取对象

Java代码Flashvars" value="clipboard=%3C!DOCTYPE%20html%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20XHTML%201.0%20Transitional%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-transitional.dtd%22%3E%20%0A%3Chtml%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml%22%20xml%3Alang%3D%22zh%22%20lang%3D%22zh%22%20dir%3D%22ltr%22%3E%20%0A%3Chead%3E%20%0A%3Cmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3Dutf-8%22%20%2F%3E%20%0A%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22lib%2Fjquery-1.3.2.min.js%22%20%3E%3C%2Fscript%3E%20%0A%3Ctitle%3E%E6%A3%80%E6%9F%A5%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E5%A4%A7%E5%B0%8F%3C%2Ftitle%3E%20%0A%3Cstyle%20type%3D%22text%2FCSS%22%3E%20%0A.img%20%7Bwidth%3A136px%3Bheight%3A102px%3B%7D%20%0A.imgError%7Bborder%3A3px%20solid%20red%3B%7D%20%0A%3C%2Fstyle%3E%20%0A%3Cscript%20type%3D%22text%2Fjavascript%22%3E%20%0A%2F%2F%E9%99%90%E5%88%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E5%A4%A7%E5%B0%8F100K%20%0Avar%20MAXSIZE%20%3D%20100%20*%201024%3B%20%0A%0A%2F%2F%E5%9B%BE%E7%89%87%E5%A4%A7%E5%B0%8F%E9%99%90%E5%88%B6%E4%BF%A1%E6%81%AF%20%0Avar%20ERROR_IMGSIZE%20%3D%20%22%E5%9B%BE%E7%89%87%E5%A4%A7%E5%B0%8F%E4%B8%8D%E8%83%BD%E8%B6%85%E8%BF%87100K%22%3B%20%0A%2F%2F%E9%BB%98%E8%AE%A4%E5%9B%BE%E7%89%87%20%0Avar%20NOPHOTO%20%3D%20%22imgs%2Fnophoto.gif%22%3B%20%0A%0A%2F%2F%E5%9B%BE%E7%89%87%E6%98%AF%E5%90%A6%E5%90%88%E6%A0%BC%20%0Avar%20isImg%20%3D%20true%3B%20%0A%2F**%20%0A*%20Input%20file%20onchange%E4%BA%8B%E4%BB%B6%20%0A*%20%40params%20obj%20file%E5%AF%B9%E8%B1%A1%20%0A*%20%40return%20void%20%0A**%2F%20%0Afunction%20checkFileChange(obj)%20%7B%20%0A%0A%2F%2F%E5%88%9D%E5%A7%8B%E5%8C%96%E8%AE%BE%E7%BD%AE%20%0A%24(%22.imgTable%22).removeClass(%22imgError%22)%3B%20%0AupdateTips(%22%22)%3B%20%0Avar%20img%20%3D%20%24(%22.img%22).get(0)%3B%20%0Avar%20file%20%3D%20obj.value%3B%20%0Avar%20exp%20%3D%20%2F.%5C.jpg%7C.%5C.gif%7C.%5C.png%7C.%5C.bmp%2Fi%3B%20%0Aif%20(exp.test(file))%20%7B%2F%2F%E9%AA%8C%E8%AF%81%E6%A0%BC%E5%BC%8F%20%0Aif(%24.browser.msie)%20%7B%2F%2F%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%98%AFIE%20%0Aimg.src%20%3D%20file%3B%20%0A%7D%20else%20%7B%20%0Aimg.src%20%3D%20obj.files%5B0%5D.getAsDataURL()%3B%20%0AsizeCheck(img)%3B%20%0A%7D%20%0A%0A%7D%20else%20%7B%20%0Aimg.src%20%3D%20NOPHOTO%3B%20%0A%24(%22.imgTable%22).addClass(%22imgError%22)%3B%20%0AupdateTips(%22%E5%9B%BE%E7%89%87%E6%A0%BC%E5%BC%8F%E4%B8%8D%E6%AD%A3%E7%A1%AE%22)%3B%20%0AisImg%20%3D%20false%3B%20%0A%7D%20%0A%0A%7D%20%0A%2F**%20%0A*%20sizeCheck%20%E6%A3%80%E6%9F%A5%E5%9B%BE%E7%89%87%E5%A4%A7%E5%B0%8F%20%0A*%20%40params%20img%20%E5%9B%BE%E7%89%87%E5%AF%B9%E8%B1%A1%20%0A*%20%40return%20void%20%0A**%2F%20%0Afunction%20sizeCheck(img)%20%7B%20%0A%2F%2F%E5%88%9D%E5%A7%8B%E5%8C%96%E8%AE%BE%E7%BD%AE%20%0A%24(%22.imgTable%22).removeClass(%22imgError%22)%3B%20%0AupdateTips(%22%22)%3B%20%0Aif%20(%24.browser.msie)%20%7B%2F%2F%E6%9F%A5%E7%9C%8B%E6%98%AF%E5%90%A6%E6%98%AFIE%20%0Aif(img.readyState%20%3D%3D%20%22complete%22)%20%7B%20%0Aif%20(img.fileSize%20%3E%20MAXSIZE)%20%7B%20%0A%24(%22.imgTable%22).addClass(%22imgError%22)%3B%20%0AupdateTips(ERROR_IMGSIZE)%3B%20%0AisImg%20%3D%20false%3B%20%0A%7Delse%20%7B%20%0AisImg%20%3D%20true%3B%20%0A%7D%20%0A%0A%7Delse%20%7B%20%0A%24(%22.imgTable%22).addClass(%22imgError%22)%3B%20%0AupdateTips(ERROR_IMGSIZE)%3B%20%0AisImg%20%3D%20false%3B%20%0A%7D%20%0A%0A%7D%20else%20%7B%20%0Avar%20file%20%3D%20%24(%22input%3Afile%5Bname%3D'uploadImg'%5D%22)%5B0%5D%3B%20%0A%0Aif%20(file.files%5B0%5D.fileSize%20%3E%20MAXSIZE)%20%7B%20%0A%24(%22.imgTable%22).addClass(%22imgError%22)%3B%20%0AupdateTips(ERROR_IMGSIZE)%3B%20%0AisImg%20%3D%20false%3B%20%0A%7Delse%20%7B%20%0AisImg%20%3D%20true%3B%20%0A%7D%20%0A%0A%7D%20%0A%7D%20%0A%0A%2F**%20%0A*%20updateTips%20%E6%B3%A8%E5%86%8C%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E6%98%BE%E7%A4%BA%20%0A*%20%40params%20str%20%E6%98%BE%E7%A4%BA%E5%86%85%E5%AE%B9%20%0A*%20%40return%20void%20%0A**%2F%20%0Afunction%20updateTips(str)%20%7B%20%0A%24(%22p%23errorTips%22).text(str)%3B%20%0A%7D%20%0A%2F**%20%0A*%20commit%20%E6%B3%A8%E5%86%8C%E6%8F%90%E4%BA%A4%20%0A*%20%40return%20void%20%0A**%2F%20%0Afunction%20commit()%20%7B%20%0A%0Avar%20isCommit%20%3D%20true%3B%20%0Avar%20usrname%20%3D%20%24(%22input%3Atext%5Bname%3D'usrname'%5D%22)%2C%20%0Aemail%20%3D%20%24(%22input%3Atext%5Bname%3D'email'%5D%22)%2C%20%0Aimg%20%3D%20%24(%22.img%22)%2C%20%0Afile%20%3D%20%24(%22input%3Afile%5Bname%3D'uploadImg'%5D%22)%2C%20%0Afrm%20%3D%20document.frm%3B%20%0A%0AisCommit%20%3D%20isCommit%20%26%26%20isImg%3B%20%0A%0Aif(isCommit)%20%7B%20%0Afrm.action%20%3D%20%22about%3Ablank%22%3B%20%0Afrm.submit()%3B%20%0A%7D%20%0A%7D%20%0A%2F**%20%0A*%20errorImg%20%E5%9B%BE%E7%89%87%E9
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表