aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies Download Multiple Files as Zip File From Gridview in C#, VB.NET

Apr 18, 2013

Here I will explain how to download multiple files as zip files archive from server using Gridview in C#, VB.NET or upload & download multiple selected files as zip archive file from Gridview in C#, VB.NET.


In previous articles I explained create zip files in, unzip files from folder in, Get Gridview row values when checkbox selected in, Delete files from uploaded folder in, create/delete directory in and many articles relating to Gridview,, C#VB.NET. Now I will explain how to upload and download multiple files as zip files folder from server in using C#, VB.NET.

To implement this first we need to get Ionic.Zip.dll from DotnetZIP Library for that check this link Ionic.Zip.dll or you can get it from attached downloadable code. Now you need to add that dll to your application bin folder for that check below steps

Go to Solution Explorer -à Right click on it and go to Add ASP.NET folder -à click on Bin

Now bin folder added to our application and place Ionic.Zip.dll in that folder and add another folder in your application (Right click on your application -à Select New folder) and give name as SampleFiles

Once you added dll and SampleFiles folder our application will be like this

Now write the following code to upload files and download selected files as zip folder from gridview

<html xmlns="">
<title>Download Multiple Files as zip files archive in using c#,</title>
<form id="form1" runat="server">
<asp:FileUpload ID="fileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload Files" onclick="btnUpload_Click" />
<br />
<asp:Label ID="lbltxt" runat="server" Font-Bold="true" ForeColor="Red" />
<asp:GridView ID="gvDetails" CellPadding="5" runat="server" AutoGenerateColumns="false">
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:BoundField DataField="Text" HeaderText="FileName" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<asp:Button ID="btnDownload" Text="Download Selected Files" runat="server" onclick="btnDownload_Click" />
Now in code behind add the following namespaces

C# Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Ionic.Zip;
Once you add namespaces write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
// Bind Data to Gridview
protected void BindGridview()
string[] filesPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string path in filesPath)
files.Add(new ListItem(Path.GetFileName(path)));
gvDetails.DataSource = files;
// insert files in folder
protected void btnUpload_Click(object sender, EventArgs e)
if (fileUpload1.HasFile)
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
string path = Server.MapPath("~/SampleFiles/" + filename);
lbltxt.Text = "File Uploaded Successfully";
// Zip all files from folder
protected void btnDownload_Click(object sender, EventArgs e)
using (ZipFile zip = new ZipFile())
foreach (GridViewRow gvrow in gvDetails.Rows)
CheckBox chk = (CheckBox) gvrow.FindControl("chkSelect");
string fileName= gvrow.Cells[1].Text ;
string filePath = Server.MapPath("~/SampleFiles/" + fileName);
zip.AddFile(filePath, "files");
Response.AddHeader("Content-Disposition", "attachment;");
Response.ContentType = "application/zip";

Imports System.Collections.Generic
Imports System.IO
Imports System.Web.UI.WebControls
Imports Ionic.Zip

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub
' Bind Data to Gridview
Protected Sub BindGridview()
Dim filesPath As String() = Directory.GetFiles(Server.MapPath("~/SampleFiles/"))
Dim files As New List(Of ListItem)()
For Each path__1 As String In filesPath
files.Add(New ListItem(Path.GetFileName(path__1)))
gvDetails.DataSource = files
End Sub
' insert files in folder
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
If fileUpload1.HasFile Then
Dim filename As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
Dim path__1 As String = Server.MapPath("~/SampleFiles/" & filename)
lbltxt.Text = "File Uploaded Successfully"
End If
End Sub
' Zip all files from folder
Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As EventArgs)
Using zip As New ZipFile()
For Each gvrow As GridViewRow In gvDetails.Rows
Dim chk As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
If chk.Checked Then
Dim fileName As String = gvrow.Cells(1).Text
Dim filePath As String = Server.MapPath("~/SampleFiles/" & fileName)
zip.AddFile(filePath, "files")
End If
Response.AddHeader("Content-Disposition", "attachment;")
Response.ContentType = "application/zip"
End Using
End Sub
End Class

To test application first upload files after that select available files in gridview and click on Download Selected Files button to generate zip file archive based on files whatever you selected Download Multiple Files as Zip File From Gridview in C#, VB.NET

Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Nice Articles

Raj said...

it nice name space problem
what can do plz help me

Anonymous said...

why you dont record videos and upload on youtube your blog is great but recording of videos is best way to explain try to look my advice.
thank you.

Anonymous said...


Anonymous said...

what will happen if am uploading files from different folders ?

Anonymous said...

nice article dude

Unknown said...

Great Article boss.....

Anonymous said...

Raj me also getting name space problem
did u get the solution to it.if yes plz tell me also brother
or any other friends who are here can tell me at

saji said...

Nice one...Very Help full

hithardenough said...

You rock my friend :) It works perfectly, not many people believe in KISS rule now a days, you do :) Many thanks.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.