<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title>文件上传与下载</title> </head> <body> <form id="form1" runat="server"> <div style="text-align: center" mce_style="text-align: center"> <table id="__01" width="1002" height="573" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <img src="images/index_01.jpg" mce_src="images/index_01.jpg" width="1002" height="108" alt=""></td> </tr> <tr> <td height="370" background="images/index_02.jpg"><table width="461" height="206" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="13"> </td> </tr> <tr> <td valign="top"><table width="437" height="148" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="3" align="center" valign="bottom" style="height: 28px"> <asp:Label ID="lblMessage" runat="server" Width="116px" ForeColor="#FF0033" /></td> </tr> <tr> <td colspan="3" align="center" valign="top" style="height: 43px"> <asp:Panel ID="Panel1" runat="server" Height="90px" ScrollBars="Auto" Width="300px"> <table id="F" runat="server" cellpadding="0" cellspacing="0" enableviewstate="true"> <tr> <td style="width: 100px; height: 30px"> <asp:FileUpload ID="fileup" runat="server" /></td> </tr> </table> </asp:Panel> </td> </tr> <tr> <td width="68" height="50"> <asp:ImageButton ID="ImageButtonAdd" runat="server" ImageUrl="~/images/tianjia.gif" OnClick="ImageButtonAdd_Click" /></td> <td colspan="2"> <a href="#" mce_href="#"></a></td> </tr> <tr> <td height="30"> <asp:ImageButton ID="ImageButtonUp" runat="server" ImageUrl="~/images/shanchuan.gif" OnClick="ImageButtonUp_Click" /> <asp:ImageButton ID="ImageButtonDown" runat="server" ImageUrl="~/images/xiazai.gif" OnClick="ImageButtonDown_Click" /></td> <td width="85" height="30"><a href="#" mce_href="#"></a></td> <td width="282"><a href="#" mce_href="#"></a></td> </tr> </table></td> </tr> </table></td> </tr> <tr> <td> <img src="images/index_03.jpg" mce_src="images/index_03.jpg" width="1002" height="95" alt=""></td> </tr> </table> </div> </form> </body> </html> using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; //源码下载www.52netweb.com public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack)//首次执行页面 { SFUPC(); } } //该方法用于当前页面上传文件控件集保存到Session中 private void SFUPC() { ArrayList AL = new ArrayList();//创建动态增加数组 foreach (Control C in F.Controls) { //在表格中查找出FileUpload控件添加到ArrayList中 if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow") { HtmlTableCell HTC = (HtmlTableCell)C.Controls[0]; foreach (Control FUC in HTC.Controls) { if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload") { FileUpload FU = (FileUpload)FUC; //添加FileUpload控件 AL.Add(FU); } } } } //把ArrayList添加到Session中 Session.Add("FilesControls", AL); } //该方法用于添加一个上传文件的控件 private void InsertC() { //实例化ArrayList ArrayList AL = new ArrayList(); this.F.Rows.Clear(); //清除id为F表格里的所有行 GetInfo(); //表示 HtmlTable 控件中的 <tr> HTML 元素 HtmlTableRow HTR = new HtmlTableRow(); //表示 HtmlTableRow 对象中的 <td> 和 <th> HTML 元素 HtmlTableCell HTC = new HtmlTableCell(); //在单元格中添加一个FileUpload控件 HTC.Controls.Add(new FileUpload()); //在行中添加单元格 HTR.Controls.Add(HTC); //在表中添加行 F.Rows.Add(HTR); SFUPC(); } //该方法用于将保存在Session中的上传文件控件集添加到表格中 private void GetInfo() { ArrayList AL = new ArrayList(); if (Session["FilesControls"] != null) { AL = (ArrayList)Session["FilesControls"]; for (int i = 0; i < AL.Count; i++) { HtmlTableRow HTR = new HtmlTableRow(); HtmlTableCell HTC = new HtmlTableCell(); HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]); HTR.Controls.Add(HTC); F.Rows.Add(HTR); } } } private void UpFile()//该方法用于执行文件上传操作 { //获取文件夹路径 string FilePath = Server.MapPath("./") + "File"; // 获取客户端上载文件的集合 HttpFileCollection HFC = Request.Files; for (int i = 0; i < HFC.Count; i++) { //访问指定的文件 HttpPostedFile UserHPF = HFC[i]; try { //判断文件是否为空 if (UserHPF.ContentLength > 0) { //将上传的文件存储在指定目录下 UserHPF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(UserHPF.FileName)); } } catch { lblMessage.Text = "上传失败!"; } } if (Session["FilesControls"] != null) { Session.Remove("FilesControls"); } lblMessage.Text = "上传成功!"; } protected void ImageButtonUp_Click(object sender, ImageClickEventArgs e) { if (this.fileup.PostedFile.FileName != "") { UpFile();//执行上传文件 SFUPC(); } else { Response.Write("<mce:script type="text/javascript"><!-- alert('上传文件不能为空!');location=Default.aspx // --></mce:script>"); } } protected void ImageButtonAdd_Click(object sender, ImageClickEventArgs e) { InsertC();//执行添加控件方法 lblMessage.Text = ""; } protected void ImageButtonDown_Click(object sender, ImageClickEventArgs e) { Response.Redirect("downFile.aspx"); } }