<%@ Import Namespace="ICSharpCode.SharpZipLib.Zip"%> <%@ Page Language="C#" Debug="true"%> <script runat="server"> void Page_Load(Object sender , EventArgs e) { } void btnSubmit_Click(Object sender, EventArgs e) { ZipFile MyZip; String strFileName; MyZip = ZipFile.Create(Server.MapPath("MyZip/thaicreate.zip")); MyZip.BeginUpdate(); //*** File1 ***// if(this.fiUpload1.HasFile) { strFileName = System.IO.Path.GetFileName(fiUpload1.FileName); //*** Save files ***// this.fiUpload1.SaveAs(Server.MapPath("MyFiles/"+fiUpload1.FileName)); //*** Zip files ***// MyZip.Add(Server.MapPath("MyFiles/"+strFileName),strFileName); } //*** File2 ***// if(this.fiUpload2.HasFile) { strFileName = System.IO.Path.GetFileName(fiUpload2.FileName); //*** Save files ***// this.fiUpload2.SaveAs(Server.MapPath("MyFiles/"+fiUpload2.FileName)); //*** Zip files ***// MyZip.Add(Server.MapPath("MyFiles/"+strFileName),strFileName); } //*** File3 ***// if(this.fiUpload3.HasFile) { strFileName = System.IO.Path.GetFileName(fiUpload3.FileName); //*** Save files ***// this.fiUpload3.SaveAs(Server.MapPath("MyFiles/"+fiUpload3.FileName)); //*** Zip files ***// MyZip.Add(Server.MapPath("MyFiles/"+strFileName),strFileName); } MyZip.CommitUpdate(); this.lblText.Text = "Zip Created <a href=MyZip/thaicreate.zip>Click here</a> to Download"; MyZip.Close(); //*** Multi FindControl Loop For ***// //ZipFile MyZip; //String strFileName; //int i; //MyZip = ZipFile.Create(Server.MapPath("MyZip/thaicreate.zip")); //MyZip.BeginUpdate(); // for(i = 1;i <= 3;i++) // { // FileUpload fiUpload = (FileUpload)this.FindControl("fiUpload"+i); // if(fiUpload != null) // { // if(fiUpload.HasFile) // { // strFileName = System.IO.Path.GetFileName(fiUpload.FileName); // //*** Save files ***// // fiUpload.SaveAs(Server.MapPath("MyFiles/"+fiUpload.FileName)); // //*** Zip files ***// // MyZip.Add(Server.MapPath("MyFiles/"+strFileName),strFileName); // } // } // } // MyZip.CommitUpdate(); //this.lblText.Text = "Zip Created <a href=MyZip/thaicreate.zip>Click here</a> to Download"; //MyZip.Close(); } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Zip Files</title> </head> <body> <form runat="server"> <asp:FileUpload id="fiUpload1" runat="server"></asp:FileUpload><br /> <asp:FileUpload id="fiUpload2" runat="server"></asp:FileUpload><br /> <asp:FileUpload id="fiUpload3" runat="server"></asp:FileUpload> <asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button> <hr /> <asp:Label id="lblText" runat="server"></asp:Label><br /> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท