Introduction
Here I will explain how to download multiple files as zip files archive from server using asp.net Gridview in C#, VB.NET or upload & download multiple selected files as zip archive file from asp.net Gridview in C#.
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
Here I will explain how to download multiple files as zip files archive from server using asp.net Gridview in C#, VB.NET or upload & download multiple selected files as zip archive file from asp.net Gridview in C#.
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();
}
}
|
0 comments :
Post a Comment