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:

using System;

using System.Collections.Generic;

using System.IO;
using System.Web.UI.WebControls;
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 :

<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
    <title>Download in zip format</title>
</
head>
<
body>
    <form id="form1" runat="server">
        <div>
            <br />
            <asp:Label ID="lbl_txt" runat="server" Font-Bold="true" ForeColor="Red" />
        </div>
        <asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk_Select" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Text" HeaderText="FileName" />
            </Columns>
            <HeaderStyle BackColor="white" Font-Bold="true" ForeColor="White" />
        </asp:GridView>
        <asp:Button ID="btn_Download" Text="Download " runat="server" OnClick="btnDownload_Click" />
    </form>
</
body>
</
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:

protected void BindGridview()
{
    
string[] files_Path = Directory.GetFiles(Server.MapPath("~/files/"));
    List<ListItem> files =
new List<ListItem>();
   
foreach (string path in files_Path)
    {
        files.Add(
new ListItem(Path.GetFileName(path)));
    }
        gridview1.DataSource = files;
        gridview1.DataBind();
    }
    Call
this function on pageload :
   
if (!IsPostBack)
    {
        BindGridview();
    }
}

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:

using (ZipFile zip = new ZipFile())
{
   
foreach (GridViewRow gr in gridview1.Rows)
    {
    CheckBox chk = (CheckBox)gr.FindControl(
"chkSelect");
   
if (chk.Checked)
    {
       
string fileName = gr.Cells[1].Text;
       
string filePath = Server.MapPath("~/files/" + fileName);
        zip.AddFile(filePath,
"files");
    }
}
Response.Clear();
Response.AddHeader(
"Content-Disposition", "attachment; filename=DownloadedFile.zip");
Response.ContentType =
"application/zip";
zip.Save(Response.OutputStream);
Response.End();
}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all