多文件上传
<body>
<form id="form1" runat="server"> <div> <asp:Panel ID="Panel1" runat="server" Height="" Width="125px"> <asp:FileUpload ID="fu" runat="server" /> </asp:Panel> <asp:TextBox ID="tb_Sum" runat="server"></asp:TextBox> <asp:Button ID="btn_Add" runat="server" Text="增加" OnClick="btn_Add_Click" /> <asp:Button ID="btn_Upload" runat="server" Text="上传全部" OnClick="btn_Upload_Click" /></div> </form> </body> 后台代码 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_Add_Click(object sender, EventArgs e) { FileUpload fu; for (int i = 0; i < Convert.ToInt32(tb_Sum.Text); i++) { fu = new FileUpload(); fu.ID = "fu_" + i.ToString(); Panel1.Controls.Add(fu); } } protected void btn_Upload_Click(object sender, EventArgs e) { string f_Name, f_Size, f_Type; HttpFileCollection hfc = Request.Files; for (int i = 0; i < hfc.Count; i++) { HttpPostedFile hpf = hfc[i]; f_Name = Path.GetFileName(hpf.FileName); f_Size = hpf.ContentLength.ToString(); f_Type = hpf.ContentType; hpf.SaveAs(Server.MapPath("../up/") + f_Name); Response.Write(f_Name + "<br>" + f_Size + "<br>" + f_Type + "<br>"); } } } |