-
前端js限制上传文件类型及大小(1)
- <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=" 500px;" onchange="fileChange(this);"/>
- </body>
- </html>
-
相关阅读:
Codeforces Round #169 (Div. 2) B. Little Girl and Game(博弈)
Codeforces Round #167 (Div. 2) C. Dima and Staircase(线段树·成段更新,繁琐)
Codeforces Round #170 (Div. 2) B. New Problem(好题)
BKDR Hash Function
DOC常用命令(转)
C++ GUI Qt4 自学笔记
windows如何取消开机启动项
如何查看电脑配置
Codeforces Round #166 (Div. 2)C. Secret(构造)
Codeforces Round #168 (Div. 2) C. kMultiple Free Set(二分查找)
-
原文地址:https://www.cnblogs.com/telwanggs/p/7809000.html
Copyright © 2020-2023
润新知