js限制上传文件类型(2)

<HTML>
<HEAD>
<TITLE>js前端控制上传文件的格式</TITLE>
</HEAD>
 
<BODY>
<FORM ACTION="load.jsp" METHOD="POST" ENCTYPE="mutipart/form-data" name="myform" οnsubmit="return check()">
请选择上传文件:<span id="span1"><INPUT TYPE="file" NAME="csv" id="csv"/></span> <img id="loading" style="display:none;" src="loading.gif"/>
<br/>
<INPUT TYPE="submit" value="提 交"/>
</FORM>
 
<script language="javascript">
function check(){
  var fileName = document.getElementById("csv").value;
  if(fileName==""){
    alert("请选择要上传的jpg文件!");
    return false;
  }
  //lastIndexOf如果没有搜索到则返回为-1
  if(fileName.lastIndexOf(".")!=-1)
  {
    var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase();
    var suppotFile = new Array();
    suppotFile[0] = "jpg";
    for(var i =0;i<suppotFile.length;i++){
      if(suppotFile[i]==fileType){
        document.getElementById("loading").style.display="";
        return true;
      } else {
        alert("不支持文件类型"+fileType);
        document.getElementById("span1").innerHTML='<INPUT TYPE="file" NAME="csv" id="csv"/>';//因为IE不支持file控件清空,所有只有重新动态的追加file控件了
        //document.getElementById("csv").value="";
        return false;
      }
    }
  }
}
</script>
</BODY>
</HTML>
另:
<pre name="code" class="html">//判断上传文件格式是否满足条件
function isPicture(fileName){
    if(fileName!=null && fileName !=""){
  //lastIndexOf如果没有搜索到则返回为-1
  if (fileName.lastIndexOf(".")!=-1) {
var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase();
var suppotFile = new Array();
suppotFile[0] = "jpg";
   suppotFile[1] = "gif";
   suppotFile[2] = "bmp";
   suppotFile[3] = "png";
   suppotFile[4] = "jpeg";
for (var i =0;i<suppotFile.length;i++) {
if (suppotFile[i]==fileType) {
return true;
} else{
 continue;
}
}
//alert("文件类型不合法,只能是jpg、gif、bmp、png、jpeg类型!");
return false;
} else{
//alert("文件类型不合法,只能是 jpg、gif、bmp、png、jpeg 类型!");
  return false;
}
}
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值