我在做导入数据,必须让用户只能选择.xls后缀的excel进行导入,请问如何控制文件后缀,避免在文件对话框中让用户可选“所有文件”?

解决方案 »

  1.   

    not possible in Internet Explorer
    you can validate the file at the server side
      

  2.   

    用js可以控制,但是本人建议在服务器端也作控制  var file_name = th.pic_url.value;
      var file_ext = file_name.substring(file_name.lastIndexOf(".")+1);
      if (th.pic_url.value!="" && th.pic_url.value!=null)
      {
        if(file_ext=="jpg" || file_ext=="gif")
        {    }
        else
        {
          alert("只允许上传.jpg和.gif类型图片文件!");
          return;
        }
      }
      

  3.   

         var last=filePath.match(/^(.*)(\.)(.{1,8})$/)[3];   //獲取文件格式
            last=last.toUpperCase();
      

  4.   

    var last=document.all.versionFile.value.match(/^(.*)(\.)(.{1,8})$/)[3];   //检查上传文件格式
                last=last.toUpperCase();
                if(last!= "APK" || last!= "apk"){  
                alert( "只能上传.apk或.APK文件,请重新选择上传文件!");
                    return false;
                 }