-->

How to Download multiple files as ZIP at once using ASP.NET & C#.

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.
  1. namespace ASPDownloadMultiFile
  2. {
  3. public class MyFile
  4. {
  5. public string FileName { get; set; }
  6. public string FilePath { get; set; }
  7. public decimal FileSize { get; set; }
  8. }
  9. }

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
  1. <h3>Multiple file download at a time using asp.net.</h3>
  2. <div>
  3. <%-- This is for show Downloadable Files --%>
  4. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  5. <Columns>
  6. <asp:BoundField HeaderText="File Name" DataField="FileName" />
  7. <asp:BoundField HeaderText="File Size (KB)" DataField="FileSize" />
  8. <asp:TemplateField>
  9. <ItemTemplate>
  10. <a href='<%#Eval("FilePath") %>' target="_blank">Download</a>
  11. </ItemTemplate>
  12. </asp:TemplateField>
  13. </Columns>
  14. </asp:GridView>
  15. <br />
  16. <asp:Button ID="btnDownloadAll" runat="server" Text="Download All Files as ZIP" OnClick="btnDownloadAll_Click" />
  17. </div>

Step-5: Write code into page load event for show data.

Write below code into Page_Load event for show files in Gridview.

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateFiles();
  6. }
  7. }

   And here is the function PopulateUploadedFiles

  1. private void PopulateFiles()
  2. {
  3. DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/DownloadFolder"));
  4. List<MyFile> allFiles = new List<MyFile>();
  5. foreach (var i in DI.GetFiles())
  6. {
  7. allFiles.Add(new MyFile
  8. {
  9. FileName = i.Name,
  10. FilePath = i.FullName.Replace(Server.MapPath("~/"),""), // For Get URL FORM Full Path
  11. FileSize = i.Length/1024
  12. }
  13. );
  14. }
  15. GridView1.DataSource = allFiles;
  16. GridView1.DataBind();
  17. }

Step-6: Add a Reference for Create ZIP.

Download ICSharpCode.SharpZipLib.dll
Right 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.

  1. protected void btnDownloadAll_Click(object sender, EventArgs e)
  2. {
  3. // Here we will create zip file & download
  4. string zipFileName = "MyZipFiles.zip";
  5. Response.ContentType = "application/zip";
  6. Response.AddHeader("content-disposition","fileName="+ zipFileName);
  7. byte[] buffer = new byte[4096];
  8. ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
  9. zipOutputStream.SetLevel(3);
  10. try
  11. {
  12. DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/DownloadFolder"));
  13. foreach (var i in DI.GetFiles())
  14. {
  15. Stream fs = File.OpenRead(i.FullName);
  16. ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(i.Name));
  17. zipEntry.Size = fs.Length;
  18. zipOutputStream.PutNextEntry(zipEntry);
  19. int count = fs.Read(buffer, 0, buffer.Length);
  20. while (count > 0)
  21. {
  22. zipOutputStream.Write(buffer, 0, count);
  23. count = fs.Read(buffer, 0, buffer.Length);
  24. if (!Response.IsClientConnected)
  25. {
  26. break;
  27. }
  28. Response.Flush();
  29. }
  30. fs.Close();
  31. }
  32. zipOutputStream.Close();
  33. Response.Flush();
  34. Response.End();
  35. }
  36. catch (Exception)
  37. {
  38. throw;
  39. }
  40. }

Step-8: Run Application.

Please give me +1 if is useful for you. Thank you.

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: