Hi comunidadmexi...,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="xls" HeaderText="xls" />
<asp:BoundField DataField="img" HeaderText="img" />
<asp:BoundField DataField="ppt" HeaderText="ppt" />
<asp:BoundField DataField="pdf" HeaderText="pdf" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Download" runat="server" OnClick="OnDownload" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.IO;
using System.Data;
using Ionic.Zip;
VB.Net
Imports System.IO
Imports Ionic.Zip
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id"),
new DataColumn("xls"),
new DataColumn("img"),
new DataColumn("ppt"),
new DataColumn("pdf")
});
dt.Rows.Add(1, @"c:\inetpub\wwwroot\xls\test.xlsx", @"c:\inetpub\wwwroot\img\test.jpg", @"c:\inetpub\wwwroot\ppt\test.pptx", @"c:\inetpub\wwwroot\pdf\test.pdf");
gvFiles.DataSource = dt;
gvFiles.DataBind();
}
}
protected void OnDownload(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
string xls = row.Cells[1].Text;
string img = row.Cells[2].Text;
string ppt = row.Cells[3].Text;
string pdf = row.Cells[4].Text;
if (!string.IsNullOrEmpty(xls))
{
if (File.Exists(xls))
{
zip.AddFile(xls, "Files");
}
}
if (!string.IsNullOrEmpty(img))
{
if (File.Exists(xls))
{
zip.AddFile(img, "Files");
}
}
if (!string.IsNullOrEmpty(ppt))
{
if (File.Exists(xls))
{
zip.AddFile(ppt, "Files");
}
}
if (!string.IsNullOrEmpty(pdf))
{
if (File.Exists(xls))
{
zip.AddFile(pdf, "Files");
}
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("xls"), New DataColumn("img"), New DataColumn("ppt"), New DataColumn("pdf")})
dt.Rows.Add(1, "c:\inetpub\wwwroot\xls\test.xlsx", "c:\inetpub\wwwroot\img\test.jpg", "c:\inetpub\wwwroot\ppt\test.pptx", "c:\inetpub\wwwroot\pdf\test.pdf")
gvFiles.DataSource = dt
gvFiles.DataBind()
End If
End Sub
Protected Sub OnDownload(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName("Files")
Dim xls As String = row.Cells(1).Text
Dim img As String = row.Cells(2).Text
Dim ppt As String = row.Cells(3).Text
Dim pdf As String = row.Cells(4).Text
If Not String.IsNullOrEmpty(xls) Then
If File.Exists(xls) Then
zip.AddFile(xls, "Files")
End If
End If
If Not String.IsNullOrEmpty(img) Then
If File.Exists(xls) Then
zip.AddFile(img, "Files")
End If
End If
If Not String.IsNullOrEmpty(ppt) Then
If File.Exists(xls) Then
zip.AddFile(ppt, "Files")
End If
End If
If Not String.IsNullOrEmpty(pdf) Then
If File.Exists(xls) Then
zip.AddFile(pdf, "Files")
End If
End If
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.End()
End Using
End Sub