美文网首页
限制文件上传格式及脚本(js)

限制文件上传格式及脚本(js)

作者: lzj01231 | 来源:发表于2021-05-26 22:16 被阅读0次
    <script>
        function fileChange(target) {
            var fileSize = 0;
            fileSize = target.files[0].size;
            var size = fileSize / 1024;
            if(size>1000){
                alert("附件不能大于1M");
                target.value="";
                return false;   //阻止submit提交
            }
            var name=target.value;
            var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
            if(fileName !="xls" && fileName !="xlsx"){
                alert("请选择Excel格式文件上传(xls、xlsx)!");
                target.value="";
                return false;   //阻止submit提交
            }
        }
    </script>
    <form  class="dropzone"  id="dropzoneForm" enctype="multipart/form-data">
        <div class="fallback">
            <input name="file" value="1M以内的Excel文件" type="file" id="file_id"  accept=".xls,.xlsx" onchange="fileChange(this);"/>
        </div>
    </form>
    

    相关文章

      网友评论

          本文标题:限制文件上传格式及脚本(js)

          本文链接:https://www.haomeiwen.com/subject/lmplsltx.html