• js检验文件格式


     HTML:
       <form id="uploadForm" method="post" class="layui-form">
       <div class="layui-form-item">
          <label class="layui-form-label">名称</label>
          <div class="layui-input-block">
             <input type="text" name="name" required lay-verify="required"
                   placeholder="请输入文件名" autocomplete="off" class="layui-input">
          </div>
       </div>
    
       <div class="layui-form-item">
          <label class="layui-form-label">资料类型:</label>
          <div class="layui-input-block">
             <select name="datatypeid"  id="datatypeid"></select>
          </div>
          <input type="hidden" id="yincang">
       </div>
       <div class="layui-form-item">
          <label class="layui-form-label">上传文件</label>
    
          <div class="layui-input-block">
             <input class="layui-input" type="file" name="file" onchange="fileChange(this);"/>
          </div>
       </div>
    
       <div class="layui-form-item">
          <div class="layui-input-block">
    
             <input type="button" class="layui-btn" value="上传"
                   onclick="upload()" />
          </div>
       </div>
    </form>
    Js 代码:
    <script type="text/javascript">
        var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
        function fileChange(target, id) {
            var fileSize = 0;
            var filetypes = [  ".xlsx", ".xls" ];//这里设置接受的文件类型
            var filepath = target.value;
            var filemaxsize = 1024 * 1;//接受的文件最大1M
            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) {
                    alertx("不接受此文件类型!");
                    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)) {
                    alertx("附件不存在,请重新输入!");
                    return false;
                }
                var file = fileSystem.GetFile(filePath);
                fileSize = file.Size;
            } else {
                fileSize = target.files[0].size;
            }
    
            var size = fileSize / 1024;
            if (size > filemaxsize) {
                alertx("附件大小不能大于" + filemaxsize / 1024 + "M!");
                target.value = "";
                return false;
            }
            if (size <= 0) {
                alertx("附件大小不能为0M!");
                target.value = "";
                return false;
            }
        }
    </script>
  • 相关阅读:
    CCF认证201809-2买菜
    git删除本地保存的账号和密码
    mysql表分区
    使用java代码将时间戳和时间互相转换
    Mysql数据库表被锁定处理
    mysql查询某个数据库表的数量
    编译nginx错误:make[1]: *** [/pcre//Makefile] Error 127
    LINUX下安装pcre出现WARNING: 'aclocal-1.15' is missing on your system错误的解决办法
    linux下安装perl
    [剑指Offer]26-树的子结构
  • 原文地址:https://www.cnblogs.com/zhaojiu/p/14636925.html
Copyright © 2020-2023  润新知