日期:2014-05-16 浏览次数:20329 次
很实用的一个JS代码,判断一个上传表单允许上传的文件类型,扩展性很好,只要改变数组里的扩展名就可判断你要的类型
?
?
<html> <head> <title>JS判断上传文件类型</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type="text/javascript"> <!-- Begin extArray = new Array(".gif", ".jpg", ".png",".txt");//允许上传的文件类型 function LimitAttach(form, file) { allowSubmit = false; if (!file) return; while (file.indexOf("\\") != -1) file = file.slice(file.indexOf("\\") + 1); ext = file.slice(file.indexOf(".")).toLowerCase(); for (var i = 0; i < extArray.length; i++) { if (extArray[i] == ext) { allowSubmit = true; break; } } if (allowSubmit) form.submit(); else alert("只能上传: "+ (extArray.join(" ")) + "\n请重新选择文件"+ "再上传."); } // End --> </script> </head> <body> <form method=post name=upform action="" enctype="multipart/form-data"> <input type=file name=uploadfile> <input type=button name="Submit" value="Submit" onclick="LimitAttach(this.form, this.form.uploadfile.value);"> </form> </body> </html>
?