注册 登录
编程论坛 JavaScript论坛

求助判断文件格式问题

thbwn 发布于 2010-02-10 21:50, 1172 次点击
现在的代码的效果是,选中文件,如果文件类型不正确,重新刷新该页.我想实现这样的效果,该怎么改?

input file 浏览选中文件,类型不正确,将该input file框自动清零,而其他input file不变.谢谢!

<html>
<head>
<title>文件传送</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {  height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt {  font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
.STYLE6 {color: #FF0000}
.STYLE8 {font-size: 12px}
-->
</style>
</head>
<body>

<script language="javascript">
function GetFilePath(obj)
  {
  //alert(obj);
  var physical = document.getElementById(obj).value;
  var length = physical.length;
  var charindex = physical.lastIndexOf(".");
  var ExtentName = physical.substr(charindex,5)
  var vExtentName = ExtentName.toLowerCase(ExtentName) //* 将扩展名转换成小写
  //*上传允许的文件类型
  if(!(vExtentName == ".txt" || vExtentName == ".ps" || vExtentName == ".rar" || vExtentName == ".zip" || vExtentName == ".doc" ))
  
  {
    alert("文件类型不正确:"+vExtentName+"");
    window.location.href = "index.htm";
   return false;
  }
  
  }

</script>

<form name="myform" method="post" action="Example_Progress.asp" enctype="multipart/form-data" target="upload">
 <table border="1" cellspacing="0" cellpadding="0" bordercolorlight="#000000" bordercolordark="#CCCCCC" width="91" height="23">
    <tr>
      <td align="left" valign="middle" height="18" width="18">&nbsp;</td>
      <td bgcolor="#FFCC00" align="left" valign="middle" height="18" width="67"> 节目表传送</td>
    </tr>
  </table>
  <br>
<table width="680" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000" bordercolorlight="#000000" bordercolordark="#CCCCCC">
    <tr bgcolor="#CCCCCC">
      <td height="22" align="left" valign="middle" bgcolor="#FFFFCC">&nbsp;<strong>节目表传送专用通道(<span class="STYLE6">合作愉快,谢谢支持!</span>) </strong></td>
    </tr>
    <tr align="left" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="42">
        <script language="javascript">
      function setid()
      {
      str='<br>';
      if(!window.myform.upcount.value)
       window.myform.upcount.value=2;
       for(i=1;i<=window.myform.upcount.value;i++)
         str+='文件'+i+':<input id="file'+i+'" type="file" name="filefield'+i+'" style="width:400" class="tx1" value="" onchange=GetFilePath("file'+i+'")><br><br>';
      window.upid.innerHTML=str+'<br>';
      }
      </script>
        <li> 需要传送的文件个数
          <input type="text" name="upcount" class="tx" value="2">
          <input type="button" name="Button" class="bt" onClick="setid();" value="· 设定 ·">
        </li>
        <br>              </td>
    </tr>
    <tr align="center" valign="middle">
      <td align="left" id="upid" height="116">  <input id=File type="file" name="filefield1" value="" onchange=GetFilePath("File")>        </td>
    <tr align="center" valign="middle" bgcolor="#eeeeee"><td bgcolor="#eeeeee" height="24">
         <input type="button" value="· 提交 ·" class="bt" onClick="UploadData()">
        <input type="reset" name="Submit2" value="· 重执 ·" class="bt"></td>
    </tr>
  </table>
</form>
<br>
</body>
</html>
<script language="javascript">
setid();
</script>
4 回复
#2
aspic2010-02-11 08:37
如果你重定向 那别的不都被清空了吗
#3
aspic2010-02-11 09:17
好像可以直接覆盖原来的OuterHTML 但是IEonly
#4
aspic2010-02-11 09:21
http://zhidao.baidu.com/question/109869690
#5
thbwn2010-02-11 19:55
谢谢,我试试
1