时间:2021-07-01 10:21:17 帮助过:11人阅读
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> var isIE = /msie/i.test(navigator.userAgent) && !window.opera; function fileChange(target,id) { var fileSize = 0; var filetypes =[".jpg",".png",".rar",".txt",".zip",".doc",".ppt",".xls",".pdf",".docx",".xlsx"]; var filepath = target.value; var filemaxsize = 1024*2;//2M if(filepath){ var isnext = false; var fileend = filepath.substring(filepath.indexOf(".")); if(filetypes && filetypes.length>0){ for(var i =0; i<filetypes.length;i++){ if(filetypes[i]==fileend){ isnext = true; break; } } } if(!isnext){ alert("不接受此文件类型!"); target.value =""; return false; } }else{ return false; } if (isIE && !target.files) { var filePath = target.value; var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); if(!fileSystem.FileExists(filePath)){ alert("附件不存在,请重新输入!"); return false; } var file = fileSystem.GetFile (filePath); fileSize = file.Size; } else { fileSize = target.files[0].size; } var size = fileSize / 1024; if(size>filemaxsize){ alert("附件大小不能大于"+filemaxsize/1024+"M!"); target.value =""; return false; } if(size<=0){ alert("附件大小不能为0M!"); target.value =""; return false; } } </script> </head> <body> <input type="file" name="contractFileName" style="width: 500px;" onchange="fileChange(this);"/> </body> </html>
相关推荐:
js判断文件类型大小并给出提示的方法
php 调用wsdl文件类型的接口代码示例
php中检测上传文件类型与上传图片大小代码
以上就是JS脚本判断在客户端大小和文件类型方法实例的详细内容,更多请关注Gxl网其它相关文章!