0

Hi,

I'm fairly new to ASP.NET with C# and I have created a GridView with a Checkbox template field which displays files in a directory on the server. I am wanting to use DotNetZip to allow the user to download selected files as a single zip file. Below is the code I have written so far but the zip file it creates appears to be empty.

Markup:

<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="File Name">
                <ItemTemplate>
                    <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnDownload" runat="server" Text="Download" 
        onclick="btnDownload_Click" />

C# Code-Behind

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ionic.Zip;

public partial class DownloadFiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var files = Directory.GetFiles(Server.MapPath("~/Uploads/Folder3"));

        gvFiles.DataSource = from f in files
                             select new
                             {
                                 FileName = Path.GetFileName(f)
                             };
        gvFiles.DataBind();
    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        string filePath = Server.MapPath("~/Uploads/Folder3/");
        string downloadFileName = "Files.zip";

        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + downloadFileName);

        using (ZipFile zip = new ZipFile())
        {
            foreach (GridViewRow row in gvFiles.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb != null && cb.Checked)
                {
                    fileName = (row.FindControl("lblFileName") as Label).Text;
                    zip.AddFile(Server.MapPath(Path.Combine(filePath, fileName)), "");
                }
            }
            zip.Save(Response.OutputStream);
        }
    }
}

I would appreciate any help.

Thanks,


James

2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by __avd
0

Add IsPostBack statement in page_load event.

protected void Page_Load(object sender, EventArgs e)
    {
     if(!IsPostBack) 
      {
        var files = Directory.GetFiles(Server.MapPath("~/Uploads/Folder3"));

        gvFiles.DataSource = from f in files
                             select new
                             {
                                 FileName = Path.GetFileName(f)
                             };
        gvFiles.DataBind();
      }
    }
0

Thanks! That solved it. I knew it would be something simple lol.

0

@jamesw,

Please mark this thread as solved if you have found an answer to your question and good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.