JS如何读取上传文件的后缀名



在点上传的的时候需要先判断文件格式是否属于允许的类型,JS实现代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">

<style type="text/css">
<!--
BODY{
BACKGROUND-COLOR: #f5feed;
font-size:9pt
}
.tx1 { height: 20px;font-size: 9pt; border: 1px solid;border-color: #000000; color: #0000FF}
-->
</style>

<SCRIPT language=javascript>
function check_file()
{
  var strFileName=form1.FileName.value;
  if (strFileName=="")
  {
   alert("请选择要上传的文件");
    return false;
  }
  varstrtype=strFileName.substring(strFileName.length-3,strFileName.length);
  strtype=strtype.toLowerCase();
  if(strtype=="jpg"||strtype=="gif"||strtype=="rar"||strtype=="zip"||strtype=="bmp"||strtype=="png"||strtype=="swf"||strtype=="doc"||strtype=="exe")
    return true;
  else{
   alert("这种文件类型不允许上传!\r\n只允许上传这几种文件:rar、zip、jpg、gif、bmp、png、swf、doc、exe\r\n请选择别的文件并重新上传。");
 form1.FileName.focus();
 return false;
  }
}
</SCRIPT>
</head>
<body leftmargin="0"topmargin="0">
<form action="#upfile.asp" method="post"name="form1" onSubmit="return check_file()"enctype="multipart/form-data">
  <input name="FileName"type="FILE" class="tx1" size="20">
  <input type="submit"name="Submit" value="上传" style="border:1px doublergb(88,88,88);font:9pt">
</form>
<br>只允许上传这几种文件:rar、zip、jpg、gif、bmp、png、swf、doc、exe
</body>
</html>

 

http://bbs.blueidea.com/thread-2659573-1-1.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值