ASP.NET(vb.net) & Upload/Multi-Upload and Zip File - This article how to example scripts ASP.NET Upload/Multiple-Upload and Zip Files
ShotDev Focus:
- ASP.NET(vb.net) & Upload/Multi-Upload and Zip File
Example
AspNetUploadAndZip.aspx
<%@ Import Namespace="ICSharpCode.SharpZipLib.Zip"%> <%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) End Sub Sub btnSubmit_Click(sender As Object, e As EventArgs) Dim MyZip As ZipFile Dim strFileName As String MyZip = ZipFile.Create(Server.MapPath("MyZip/shotdev.zip")) With MyZip .BeginUpdate() '*** File1 ***' If Me.fiUpload1.HasFile Then strFileName = System.IO.Path.GetFileName(fiUpload1.FileName) '*** Save files ***' Me.fiUpload1.SaveAs(Server.MapPath("MyFiles/"&fiUpload1.FileName)) '*** Zip files ***' .Add(Server.MapPath("MyFiles/"&strFileName),strFileName) End IF '*** File2 ***' If Me.fiUpload2.HasFile Then strFileName = System.IO.Path.GetFileName(fiUpload2.FileName) '*** Save files ***' Me.fiUpload2.SaveAs(Server.MapPath("MyFiles/"&fiUpload2.FileName)) '*** Zip files ***' .Add(Server.MapPath("MyFiles/"&strFileName),strFileName) End IF '*** File3 ***' If Me.fiUpload3.HasFile Then strFileName = System.IO.Path.GetFileName(fiUpload3.FileName) '*** Save files ***' Me.fiUpload3.SaveAs(Server.MapPath("MyFiles/"&fiUpload3.FileName)) '*** Zip files ***' .Add(Server.MapPath("MyFiles/"&strFileName),strFileName) End IF .CommitUpdate() End With Me.lblText.Text = "Zip Created <a href=MyZip/shotdev.zip>Click here</a> to Download" MyZip.Close() '*** Multi FindControl Loop For ***' 'Dim MyZip As ZipFile 'Dim strFileName As String 'Dim i As Integer 'MyZip = ZipFile.Create(Server.MapPath("MyZip/shotdev.zip")) 'With MyZip ' .BeginUpdate() ' For i = 1 To 3 ' Dim fiUpload As FileUpload = Me.FindControl("fiUpload"&i) ' IF Not IsNothing(fiUpload) Then ' If fiUpload.HasFile Then ' strFileName = System.IO.Path.GetFileName(fiUpload.FileName) ' '*** Save files ***' ' fiUpload.SaveAs(Server.MapPath("MyFiles/"&fiUpload.FileName)) ' '*** Zip files ***' ' .Add(Server.MapPath("MyFiles/"&strFileName),strFileName) ' End IF ' End IF ' Next ' ' .CommitUpdate() 'End With 'Me.lblText.Text = "Zip Created <a href=MyZip/shotdev.zip>Click here</a> to Download" 'MyZip.Close() End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</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>
Screenshot