<html> 
<head> 
<title> 
FileUpLoad 
</title> 
<script type="text/javascript" language= 'javascript ' > 
    function checkData() 
    { 
      if (document.faxForm.f_sendTo.value== ' ') { 
        alert( '收件人不能为空! '); 
        document.faxForm.f_sendTo.focus(); 
        return false; 
      } 
      if (document.faxForm.f_title.value== ' ') { 
        alert( '标题不能为空! '); 
        document.faxForm.f_title.focus(); 
        return false; 
      } 
      return true; 
    }     function setValue() 
    { 
      var file = "f"+document.faxForm.count.value; 
      var address = document.getElementById(file).value; 
      var div=document.createElement("div"); 
      var count = document.faxForm.count.value; 
      div.id="s_"+count; 
      div.name="s_"+count; 
      var fileName = address.substring(address.lastIndexOf("\\")+1,address.length); 
      div.innerHTML = fileName+" <a href=\"#\" onclick=\"del( '"+count+"');\">× </a>"+";"; 
      document.getElementById("show").appendChild(div); 
      document.faxForm.count.value=Number(count)+1; 
    }     function addFiles() 
    { 
      var div=document.createElement("div"); 
      var count = document.faxForm.count.value; 
      var file = "f"+count; 
      div.id="f_"+count; 
      div.name="f_"+count; 
      if(file != "f0") 
      { 
        var div2=document.createElement("div"); 
        div2.id="f_"+Number(count)+1; 
        div2.name="f_"+Number(count)+1; 
        div2.innerHTML = " <input type=\"file\" id=\"f"+(Number(count)+1)+"\" name=\"f"+(Number(count)+1)+"\" style= 'display:none; ' onchange=\"setValue()\"/>"; 
        document.getElementById("show").appendChild(div2); 
      }else{ 
        div.innerHTML = " <input type=\"file\" id=\"f1\" name=\"f1\" style= 'display:none; ' onchange=\"setValue()\"/>"; 
      } 
      document.getElementById("show").appendChild(div); 
      document.getElementById(file).click(); 
    }     function del(id) 
    { 
      var f = document.getElementById("f_"+id); 
      document.getElementById("show").removeChild(f); 
      var s = document.getElementById("s_"+id); 
      document.getElementById("show").removeChild(s); 
    } 
</script> 
</head> 
<link rel="stylesheet" type="text/css" href="/Fax/include/1/style.css"> 
<body  class="bodycolor"> 
      <form name="faxForm" onSubmit="return checkData()" method="post" enctype="multipart/form-data" action="/Fax/sendFaxAction.do"> 
        <table  width= '100% ' class="small" border=0 cellspacing=0 cellpadding=2 
         bgcolor="#E1F8FA"> 
           <tr align="center"> 
            <td width="20%" align="left" class="Big">&nbsp; 收件人 <a href="#"> </td> 
             <td width="80%" align="left" class="Big">&nbsp;&nbsp; <input type="text" name="f_sendTo"/> </td> 
          </tr> 
          <tr align="center"> 
            <td width="20%" align="left" class="Big">&nbsp; 标题 </td> 
            <td width="80%" align="left" class="Big">&nbsp;&nbsp; <input type="text" name="f_title"/> </td> 
          </tr> 
          <tr align="center"> 
            <td width="20%" align="left" class="Big"> &nbsp;&nbsp;备注 </td> 
            <td width="80%" align="left" class="Big">&nbsp;&nbsp; <textarea cols="50" rows="4" name="f_re"> </textarea> </td> 
          </tr> 
          <tr align="center"> 
            <td width="20%" align="left" class="Big"> 
               &nbsp;&nbsp; <a href="#" onClick="addFiles();">添加附件 </a>:&nbsp;&nbsp; 
              </td> 
              <td> 
                <input type="file" name="f0" id="f0" style="display:none;" onChange="setValue()"> 
              <div align="left" id="show"> </div> 
              <input type="hidden" name="count" value="0"/> 
            </td> 
          </tr> 
          <tr align="left"> 
            <td class=tiny width="10%" align="left" colSpan=2> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit"  value="发送"/> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  <input name="reset" type="reset" class="BigButton" value="清空"/> 
            </td> 
          </tr> 
        </table> 
      </form> 
</body> 
</html> 

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "f"+ TotalFiles.toString();
    file.onchange = function ()
    {
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    };
    $("UpFilePanelHidden").appendChild(file); var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);

    $("f" + TotalFiles.toString()).click();
    }
    function SetValue(str)
    {
    $("s"+str).innerHTML = $("f"+str).value.substring($("f"+str).value.lastIndexOf("\\")+1,$("f"+str).value.length); 
    }
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    }
    </script>
    <body>
    <div id="UpFilePanelHidden" style="display:none;"></div>
    <div id="UpFilePanelShow"></div>
    <input name="" type="button" value="添加附件"  onclick="AddFiles();" />
    </body>
    </html>
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "f"+ TotalFiles.toString();
    file.onchange = function ()
    {
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    };
    $("UpFilePanelHidden").appendChild(file); var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);

    $("f" + TotalFiles.toString()).click();
    }
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    }
    </script>
    <body>
    <div id="UpFilePanelHidden" style="display:none;"></div>
    <div id="UpFilePanelShow"></div>
    <input name="" type="button" value="添加附件"  onclick="AddFiles();" />
    </body>
    </html>看你写的好乱,自己写了个
    在IE6下测试通过关键在于要用appendChild
    不要用xxx.innerHTML = xxx.innerHTML + xxx;
    调试的时候把<div id="UpFilePanelHidden" style="display:none;"></div>改成<div id="UpFilePanelHidden" style="display:;"></div>就可以了
      

  3.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "f"+ TotalFiles.toString(); //这句可改成 file.name = "files" 方便后台取值 后台会得到files的数组
    file.onchange = function ()
    {
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    };
    $("UpFilePanelHidden").appendChild(file); var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);

    $("f" + TotalFiles.toString()).click();
    }
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    //TotalFiles --;
    }
    </script>
    <body>
    <div id="UpFilePanelHidden" style="display:none;"></div>
    <div id="UpFilePanelShow"></div>
    <input name="" type="button" value="添加附件"  onclick="AddFiles();" />
    </body>
    </html>
      

  4.   

    littlelam 你写是比我写得好多了
    但是最主要的问题还是没有解决
    我不知道你试了没有
    如果加个form的话
    点提交的时候就会先清空所有file里面的值
    所有的都清空完了才提交
    fileitem是空的
    所以就上传不了
      

  5.   

    其实你的实现的功能跟我的是一样的
    只是我的写得太乱了
    不隐藏file的话就可以看到
    要清空所有的file之后才提交
      

  6.   

    所有的file
    就是说如果你先了三个附件的话
    第一次点提交的时候第一个file里的值就没了
    光标在第一个file里面
    第二次点的时候……
    直到三个file都没值了
    才会提交
      

  7.   

    这样的话fileitem根本就不可能有东西啦
    就不能上传啦
      

  8.   

    自己手动的去触发file的click事件好像不行
    如果是点浏览按钮触发的就可以提交
    不知道什么原因
    有哪位高手知道不
      

  9.   

    难道只能点file的浏览按钮才可以正常提交
    为什么?
    不都要调用click吗
      

  10.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126网络硬盘上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "files"+ TotalFiles.toString(); 
    file.hideFocus = true;
    file.style.cssText = "cursor:pointer; width:1px; border:none; height:32px; padding:0px; margin:0px;position:absolute; opacity:0;filter:alpha(opacity=0);font-size:20px;";
    file.onchange = function ()
    {
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    this.style.display = "none";
    AddFiles();
    };
    $("UpFilePanelHidden").appendChild(file);
    var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);}
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    }
    function DelEmpty()
    {
    for (var i=1;i<=TotalFiles;i++)
    {
    if ($("f"+i.toString()))
    {
    if ($("f"+i.toString()).value == "")
    $("UpFilePanelHidden").removeChild($("f"+i.toString()));
    }

    }
    }
    </script>
    <body>
    <FORM METHOD="post" NAME="form1" ACTION="savetofile.asp" ENCTYPE="multipart/form-data" onsubmit="DelEmpty();return true;">
    <div style="width:100px; height:30px;background-image:url(http://mimg.163.com/netfolder/images/all_toolbar.gif);background-position:2px -64px; margin:6px 4px; border:1px solid #bebebe; cursor:pointer; "><a id="UpFilePanelHidden" href="javascript:void(0);" ></a></div>
    <div id="UpFilePanelShow"></div><script>AddFiles();</script>
    <br />
    <input type="submit" name="Submit" value="提交" />
    </form>
    </body>
    </html>这个应该可以了
      

  11.   

    我在本机上测试可以上传
    难道只能点file的浏览按钮才可以正常提交 
    为什么? 
    不都要调用click吗
    =============
    这个可能是IE为了安全性设计不允许的
    跟不能设默认值一样,防止服务端向客户端取资源
      

  12.   


    兼容Firefox
    [code=HTML]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126网络硬盘上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "files"+ TotalFiles.toString(); 
    file.hideFocus = true;
    file.size = 1;
    file.style.cssText = "cursor:pointer; width:1px; border:none; height:32px; padding:0px; margin:0px;position:absolute; opacity:0;filter:alpha(opacity=0);left:-32px !important;left:-5px;left/**/:-3px; font-size:20px;";
    file.onchange = function ()
    {
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    this.style.display = "none";
    AddFiles();
    };
    $("UpFilePanelHidden").appendChild(file);
    var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);}
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    }
    function DelEmpty()
    {
    for (var i=1;i<=TotalFiles;i++)
    {
    if ($("f"+i.toString()))
    {
    if ($("f"+i.toString()).value == "")
    $("UpFilePanelHidden").removeChild($("f"+i.toString()));
    }

    }
    }
    </script>
    <body>
    <FORM METHOD="post" NAME="form1" ACTION="savetofile.asp" ENCTYPE="multipart/form-data" onsubmit="DelEmpty();return true;">
    <div style="width:100px; height:30px;background-image:url(http://mimg.163.com/netfolder/images/all_toolbar.gif);background-position:2px -64px; margin:6px 4px; border:1px solid #bebebe; cursor:pointer; "><a id="UpFilePanelHidden" href="javascript:void(0);" ></a></div>
    <div id="UpFilePanelShow"></div><script>AddFiles();</script>
    <br />
    <input type="submit" name="Submit" value="提交" />
    </form>
    </body>
    </html>[/code]
      

  13.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>仿126网络硬盘上传 by littlelam</title>
    </head>
    <script>
    var TotalFiles = 0;
    function AddFiles()
    {
    TotalFiles ++;
    var file=document.createElement("input"); 
    file.type = "file";
    file.id = "f"+ TotalFiles.toString();
    file.name = "files"+ TotalFiles.toString(); 
    file.hideFocus = true;
    file.size = 1;
    file.style.cssText = "cursor:pointer; width:1px; border:none; height:32px; padding:0px; margin:0px;position:absolute; opacity:0;filter:alpha(opacity=0);left:-32px !important;left:-5px;left/**/:-3px; font-size:20px;";
    file.onchange = function ()
    {
    for (var i=1;i<=TotalFiles;i++)
    {
    if ($("f"+i.toString())&&$("f"+i.toString())!=this)
    {
    if ($("f"+i.toString()).value == this.value)
    {
    DelFile(this.id.replace("f",""));
    AddFiles();
    window.alert("已有相同文件,无须重复上传");
    return;
    }

    }

    }
    $("s"+TotalFiles.toString()).innerHTML = $("f"+TotalFiles.toString()).value.substring($("f"+TotalFiles.toString()).value.lastIndexOf("\\")+1,$("f"+TotalFiles.toString()).value.length) + "    <span onclick=\"DelFile('"+TotalFiles.toString()+"')\" style=\"cursor:pointer;color:red\">X</span>";
    this.style.display = "none";
    AddFiles();
    };
    $("UpFilePanelHidden").appendChild(file);
    var div=document.createElement("div"); 
    div.id = "s"+ TotalFiles.toString();
    $("UpFilePanelShow").appendChild(div);}
    function $(str)
    {
    return document.getElementById(str);
    }
    function DelFile(str)
    {
    $("UpFilePanelHidden").removeChild($("f"+str));
    $("UpFilePanelShow").removeChild($("s"+str));
    }
    function DelEmpty()
    {
    for (var i=1;i<=TotalFiles;i++)
    {
    if ($("f"+i.toString()))
    {
    if ($("f"+i.toString()).value == "")
    $("UpFilePanelHidden").removeChild($("f"+i.toString()));
    }

    }
    }
    </script>
    <body>
    <FORM METHOD="post" NAME="form1" ACTION="savetofile.asp" ENCTYPE="multipart/form-data" onsubmit="DelEmpty();return true;">
    <div style="width:100px; height:30px;background-image:url(http://mimg.163.com/netfolder/images/all_toolbar.gif);background-position:2px -64px; margin:6px 4px; border:1px solid #bebebe; cursor:pointer; "><a id="UpFilePanelHidden" href="javascript:void(0);" ></a></div>
    <div id="UpFilePanelShow"></div><script>AddFiles();</script>
    <br />
    <input type="submit" name="Submit" value="提交" />
    </form>
    </body>
    </html>