Introduction:
Description:
In
previous articles I explained create zip files in asp.net, unzip files from folder in asp.net, Get Gridview row values when checkbox
selected in asp.net, Delete files from uploaded folder in
asp.net,
create/delete directory in asp.net and many articles
relating to Gridview, SQL, jQuery, asp.net, C#, VB.NET. Now I will explain how
to upload and download multiple files as zip files folder from server in asp.net 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="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Download Multiple Files as zip files archive in Asp.net
  using c#,vb.net</title> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
<div> 
<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"
  /> 
</div> 
<asp:GridView ID="gvDetails"
  CellPadding="5"
  runat="server"
  AutoGenerateColumns="false"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<asp:CheckBox ID="chkSelect"
  runat="server"
  /> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:BoundField DataField="Text" HeaderText="FileName" /> 
</Columns> 
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /> 
</asp:GridView> 
<asp:Button ID="btnDownload"
  Text="Download
  Selected Files" runat="server" onclick="btnDownload_Click" /> 
</form> 
</body> 
</html> | 
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) 
{ 
if(!IsPostBack) 
{ 
BindGridview(); 
} 
} 
// 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; 
gvDetails.DataBind(); 
} 
// 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); 
fileUpload1.SaveAs(path); 
lbltxt.Text = "File
  Uploaded Successfully"; 
BindGridview(); 
} 
} 
// 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"); 
if(chk.Checked) 
{ 
string fileName= gvrow.Cells[1].Text ; 
string filePath = Server.MapPath("~/SampleFiles/" + 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(); 
} 
} | 
VB.NET
Code
| 
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 
BindGridview() 
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))) 
Next 
gvDetails.DataSource = files 
gvDetails.DataBind() 
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) 
fileUpload1.SaveAs(path__1) 
lbltxt.Text = "File
  Uploaded Successfully" 
BindGridview() 
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 
Next 
Response.Clear() 
Response.AddHeader("Content-Disposition",
  "attachment;
  filename=DownloadedFile.zip") 
Response.ContentType = "application/zip" 
zip.Save(Response.OutputStream) 
Response.[End]() 
End Using 
End Sub 
End Class | 
Demo
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
Sample Code Attached
| If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. | |||
|  Subscribe by RSS  Subscribe by Email | |||



 
10 comments :
Nice Articles
it nice name space problem
what can do plz help me
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.
excellent
what will happen if am uploading files from different folders ?
nice article dude
Great Article boss.....
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 bilalbinamar@rediff.com
Nice one...Very Help full
You rock my friend :) It works perfectly, not many people believe in KISS rule now a days, you do :) Many thanks.
Note: Only a member of this blog may post a comment.