<%@ Page Language="C#" Debug="true" %> <script runat="server"> void ddlNum_Changed(Object sende, System.EventArgs e) { int i; FileUpload fileUpload; Label lblBr; for( i = 1; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value);i++) { fileUpload = new FileUpload(); fileUpload.ID = "fiUpload" + i; lblBr = new Label(); lblBr.Text = "<br>"; this.pnlCtrl.Controls.Add(fileUpload); this.pnlCtrl.Controls.Add(lblBr); } if(Convert.ToInt32(this.ddlNum.SelectedItem.Value) > 0) { this.btnUpload.Visible = true; } else { this.btnUpload.Visible = false; } this.form1.Enctype = "multipart/form-data"; } void btnUpload_OnClick(Object sender, EventArgs e) { int i; HttpFileCollection myUpoad = Request.Files; HttpPostedFile myFiles; for( i = 0; i<= Convert.ToInt32(this.ddlNum.SelectedItem.Value)-1;i++) { myFiles = (HttpPostedFile)myUpoad[i]; if(myUpoad.Keys[i].ToString() == ("fiUpload" + (i + 1)).ToString()) //*** Check Control ***// { if(myFiles.FileName.ToString() != "") { String strFileName = System.IO.Path.GetFileName(myFiles.FileName); myFiles.SaveAs(Server.MapPath("Myfiles/" + strFileName)); this.lblText.Text = this.lblText.Text + strFileName + " Uploaded.<br>"; } } } this.lblText.Visible = true; this.btnUpload.Visible = false; this.ddlNum.Visible = false; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Upload files</title> </head> <body> <form id="form1" runat="server"> <asp:DropDownList id="ddlNum" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlNum_Changed"> <asp:ListItem Text="0" Value="0"></asp:ListItem> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> <asp:ListItem Text="4" Value="4"></asp:ListItem> <asp:ListItem Text="5" Value="5"></asp:ListItem> <asp:ListItem Text="6" Value="6"></asp:ListItem> <asp:ListItem Text="7" Value="7"></asp:ListItem> <asp:ListItem Text="8" Value="8"></asp:ListItem> </asp:DropDownList> <asp:Panel id="pnlCtrl" runat="server"></asp:Panel> <input id="btnUpload" Visible="false" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" /> <asp:Label id="lblText" runat="server"></asp:Label> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท