RT

解决方案 »

  1.   

    页面:
    js:
     function AddAttachments()
                {    
                     if(num > 5)
                     {
                        alert("最多只能上传五个附件!");
                        return;
                     }                
                     
                     num ++;
                        
                     document.getElementById('attach').innerText = "继续添加附件";     
                     tb = document.getElementById('attAchments');
                     newRow = tb.insertRow();
                     newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'>&nbsp;&nbsp;<input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";         
                }
                function delFile(index)
                {    
                     num --;
                     document.getElementById('attAchments').deleteRow(index);
                     tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件"; 
                }    html <table id="attAchments">
                            </table>
                            <a id="attach" style="font-family: 宋体; font-size: 9pt;" title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件单个大小不要超过50M。" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
                           <asp:TextBox ID="TextBox1" Runat="server" Font-Bold="true"  class="rece4" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnSend" runat="server" Text=" 上传 " OnClick="btnSend_Click"></asp:Button>后台:
      protected void btnSend_Click(object sender, EventArgs e)
            {            StringBuilder sb = new StringBuilder();            int attCount = 0;            string filePath = "";            string strPath = "";            string Name = "";
                string newName = "";            for (int i = 0; i < Request.Files.Count; i++)
                {                if (Request.Files[i].ContentLength > 0)
                    {                    filePath = Request.Files[i].FileName;                    FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);                    double d = file.Length / 1024 / 1024;                    sb.Append("Files" + attCount++ + ": " + filePath + "<br>");                    Name = getName(filePath);
                        newName = getNewName(Name);
                        strPath = Server.MapPath("~/mailsfile/") + filePath.Substring(filePath.LastIndexOf("\\") + 1).Replace(Name, newName);
                      
                        //保存文件
                        Request.Files[0].SaveAs(strPath);                 
                    }            }            //sb.Insert(0, "you upload " + attCount + " files.<br>"); Response.Write(sb.ToString());        }
      

  2.   

    参考:http://blog.csdn.net/baozhenliang/archive/2006/11/25/1413492.aspx