Bind Files in Gridview and Download in ZIP Folder in ASP.NET

Introduction

In this article I explain how to bind files in a GridView and how to download selected files in zip format.

Requirements

The requirements are:

  1. Dll Ionic.Zip.dll
  2. The following namespace:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.IO;  
    4. using System.Web.UI.WebControls;  
    5. using Ionic.Zip;

Add the Ionic.Zip.dll to your bin folder of the application then add the namespaces above.

Code .aspx :

I have some files in my application, file1.txt and file2.doc. I bind these files in the GridView. But before binding to the GridView I design my page. Use the following code to understand what I have done :

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3.     <title>Download in zip format</title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <div>  
  8.             <br />  
  9.             <asp:Label ID="lbl_txt" runat="server" Font-Bold="true" ForeColor="Red" />  
  10.         </div>  
  11.         <asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">  
  12.             <Columns>  
  13.                 <asp:TemplateField>  
  14.                     <ItemTemplate>  
  15.                         <asp:CheckBox ID="chk_Select" runat="server" />  
  16.                     </ItemTemplate>  
  17.                 </asp:TemplateField>  
  18.                 <asp:BoundField DataField="Text" HeaderText="FileName" />  
  19.             </Columns>  
  20.             <HeaderStyle BackColor="white" Font-Bold="true" ForeColor="White" />  
  21.         </asp:GridView>  
  22.         <asp:Button ID="btn_Download" Text="Download " runat="server" OnClick="btnDownload_Click" />  
  23.     </form>  
  24. </body>  
  25. </html>
Here you can see I have taken a GridView with a checkbox and a button with the text "Download".

Code.cs :

Bind files in GridView:
  1. protected void BindGridview()  
  2. {  
  3.     string[] files_Path = Directory.GetFiles(Server.MapPath("~/files/"));  
  4.     List<ListItem> files = new List<ListItem>();  
  5.     foreach (string path in files_Path)  
  6.     {  
  7.         files.Add(new ListItem(Path.GetFileName(path)));  
  8.     }  
  9.         gridview1.DataSource = files;  
  10.         gridview1.DataBind();  
  11.     }  
  12.     Call this function on pageload :  
  13.     if (!IsPostBack)  
  14.     {  
  15.         BindGridview();  
  16.     }  
  17. }  

Now, when you save all these and view the page in a browser you will see all files in the GridView.

Now write the following code for the Download button click:

  1. using (ZipFile zip = new ZipFile())  
  2. {  
  3.     foreach (GridViewRow gr in gridview1.Rows)  
  4.     {  
  5.     CheckBox chk = (CheckBox)gr.FindControl("chkSelect");  
  6.     if (chk.Checked)  
  7.     {  
  8.         string fileName = gr.Cells[1].Text;  
  9.         string filePath = Server.MapPath("~/files/" + fileName);  
  10.         zip.AddFile(filePath, "files");  
  11.     }  
  12. }  
  13. Response.Clear();  
  14. Response.AddHeader("Content-Disposition""attachment; filename=DownloadedFile.zip");  
  15. Response.ContentType = "application/zip";  
  16. zip.Save(Response.OutputStream);  
  17. Response.End();  
  18. }  
When you write this code you are done with the code to download selected files in zipformat. Just save all again and view the page in the browser; it will work fine.


Similar Articles