Introduction
In this post I am explain how to Download multiple files as ZIP at once using ASP.NET & C#.Here I have used ICSharpCode.SharpZipLib.dll to Create ZIP file.
Steps :
Step - 1: Create New Project.
Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.Step-2: Add a Class.
Right Click on Solution Explorer > Add > Class > Enter Class Name > Add.
Here is the class.
namespace ASPDownloadMultiFile { public class MyFile { public string FileName { get; set; } public string FilePath { get; set; } public decimal FileSize { get; set; } } }
Step-3: Add New Folder.
Right Click on Solution Explorer > Add > New Folder > Rename Folder.Step-4: Add a Webpage and Design for Show files.
Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.HTML Code
<h3>Multiple file download at a time using asp.net.</h3> <div> <%-- This is for show Downloadable Files --%> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="File Name" DataField="FileName" /> <asp:BoundField HeaderText="File Size (KB)" DataField="FileSize" /> <asp:TemplateField> <ItemTemplate> <a href='<%#Eval("FilePath") %>' target="_blank">Download</a> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Button ID="btnDownloadAll" runat="server" Text="Download All Files as ZIP" OnClick="btnDownloadAll_Click" /> </div>
Step-5: Write code into page load event for show data.
Write below code into Page_Load event for show files in Gridview.protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateFiles(); } }
private void PopulateFiles() { DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/DownloadFolder")); List<MyFile> allFiles = new List<MyFile>(); foreach (var i in DI.GetFiles()) { allFiles.Add(new MyFile { FileName = i.Name, FilePath = i.FullName.Replace(Server.MapPath("~/"),""), // For Get URL FORM Full Path FileSize = i.Length/1024 } ); } GridView1.DataSource = allFiles; GridView1.DataBind(); }
Step-6: Add a Reference for Create ZIP.
Download ICSharpCode.SharpZipLib.dllRight Click on References under solution explorar > Add Reference... > Browse > Select ICSharpCode.SharpZipLib.dll > Add > Ok.
Step-7: Write code for Download multiple files at once.
Write below code into button click event for download multiple files.protected void btnDownloadAll_Click(object sender, EventArgs e) { // Here we will create zip file & download string zipFileName = "MyZipFiles.zip"; Response.ContentType = "application/zip"; Response.AddHeader("content-disposition","fileName="+ zipFileName); byte[] buffer = new byte[4096]; ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream); zipOutputStream.SetLevel(3); try { DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/DownloadFolder")); foreach (var i in DI.GetFiles()) { Stream fs = File.OpenRead(i.FullName); ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(i.Name)); zipEntry.Size = fs.Length; zipOutputStream.PutNextEntry(zipEntry); int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!Response.IsClientConnected) { break; } Response.Flush(); } fs.Close(); } zipOutputStream.Close(); Response.Flush(); Response.End(); } catch (Exception) { throw; } }