• H5 Input=file 文件类型大小限制


     1 <html>
     2 <head>
     3     <meta charset="UTF-8">
     4     <title>Document</title>
     5 </head>
     6 <body>
     7 <input type="file" name="contractFileName" style=" 500px;" onchange="fileChange(this);"/>
     8 
     9 <script type="text/javascript">
    10     var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    11     function fileChange(target,id) {
    12         var fileSize = 0;
    13         var filetypes =[".jpg",".png",".rar",".txt",".zip",".doc",".ppt",".xls",".pdf",".docx",".xlsx"];
    14         var filepath = target.value;
    15         var filemaxsize = 1024*2;//2M
    16         if(filepath){
    17             var isnext = false;
    18             var fileend = filepath.substring(filepath.lastIndexOf("."));
    19             if(filetypes && filetypes.length>0){
    20                 for(var i =0; i<filetypes.length;i++){
    21                     if(filetypes[i]==fileend){
    22                         isnext = true;
    23                         break;
    24                     }
    25                 }
    26             }
    27             if(!isnext){
    28                 alert("不接受此文件类型!");
    29                 target.value ="";
    30                 return false;
    31             }
    32         }else{
    33             return false;
    34         }
    35         if (isIE && !target.files) {
    36             var filePath = target.value;
    37             var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
    38             if(!fileSystem.FileExists(filePath)){
    39                 alert("附件不存在,请重新输入!");
    40                 return false;
    41             }
    42             var file = fileSystem.GetFile (filePath);
    43             fileSize = file.Size;
    44         } else {
    45             fileSize = target.files[0].size;
    46         }
    47 
    48         var size = fileSize / 1024;
    49         if(size>filemaxsize){
    50             alert("附件大小不能大于"+filemaxsize/1024+"M!");
    51             target.value ="";
    52             return false;
    53         }
    54         if(size<=0){
    55             alert("附件大小不能为0M!");
    56             target.value ="";
    57             return false;
    58         }
    59     }
    60 </script>
    61 
    62 </body>
    63 </html>
  • 相关阅读:
    http://localhost:8080/ 演出Oracle说明
    JS浏览器类型推断方法
    MVC设计模式JavaWeb实现
    《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定
    创建表单
    道路软件质量:SourceMonitor
    HDU
    HDU 3032 Nim or not Nim? (sg函数求解)
    OpenWRT推理client线上的数
    IIS的ISAPI接口简介
  • 原文地址:https://www.cnblogs.com/qiupiaohujie/p/13536189.html
Copyright © 2020-2023  润新知