• 转:input file 文件上传,js控制上传文件的大小和格式


    https://www.haorooms.com/post/js_inputfileupload

    javascript来控制文件上传的大小和类型。

    <form  action="后端接口" enctype="multipart/form-data" method="post" id="attachment_uploads">
      <div class="attachs fl">
        <div class="t_fjfont">附件:</div>
        <div class="upload_btns"><input type="file" name="file"  id="file" onchange="fileChange(this);" /> </div>
      </div>
      <div class="attachs fl">
        <div class="t_fjfont" style="color:red;font-size:12px;font-weight:bold">文件上限2MB</d
      </div>
      <div class="attachs fl">
        <div class="upload_btns"><input type="file" name="files"  id="file_fujian" onchange="filefujianChange(this);" /> <input type="hidden" name="rev" class="hide_rev" /></div>
      </div>
    </form>
    
    
    var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    function fileChange(target) {
      var fileSize = 0;         
      if (isIE && !target.files) {     
        var filePath = target.value;     
        var fileSystem = new ActiveXObject("Scripting.FileSystemObject");        
        var file = fileSystem.GetFile (filePath);     
        fileSize = file.Size;    
      } else {    
        fileSize = target.files[0].size;     
      }   
      var size = fileSize / 1024;    
      if(size>2000){  
        alert("附件不能大于2M");
        target.value="";
        return
      }
      var name=target.value;
      var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();
      if(fileName !="xls" && fileName !="xlsx"){
        alert("请选择execl格式文件上传!");
        target.value="";
        return
      }
    }
    function filefujianChange(target) {   var fileSize = 0;   if (isIE && !target.files) {     var filePath = target.value;     var fileSystem = new ActiveXObject("Scripting.FileSystemObject");     var file = fileSystem.GetFile (filePath);     fileSize = file.Size;   } else {     fileSize = target.files[0].size;   }   var size = fileSize / 1024;   if(size>2000){     alert("附件不能大于2M");     target.value="";     return   }   var name=target.value;   var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();   if(fileName !="jpg" && fileName !="jpeg" && fileName !="pdf" && fileName !="png" && fileName !="dwg" && fileName !="gif" ){     alert("请选择图片格式文件上传(jpg,png,gif,dwg,pdf,gif等)!");     target.value="";     return   } }
  • 相关阅读:
    Release COM Objects in AE
    图像相关系数
    Geoprocessor edit the featureclasses in memmory
    NetLogo AStar path finding
    IDL+C#三种调用方式
    Dictionary is not like a array
    C# DataGridView 禁止列排序
    工作总结
    (转)常见数据库设计(1)——字典数据
    碎碎念(3)
  • 原文地址:https://www.cnblogs.com/chuningning/p/9140528.html
Copyright © 2020-2023  润新知