Add selected files from GridView

Jan 6, 2011 at 12:56 AM


I am fairly new to (with C#) and have a GridView that displays files in a folder on the server along with a checkbox template field. I am trying to use DotNetZip to add selected files from the GridView to a zip file for the user to download. Below is the code I have so far, but the zip file it creates appears to be empty.

GridView Markup:

<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
                <asp:CheckBox ID="chkSelect" runat="server" />
        <asp:TemplateField HeaderText="File Name">
                <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />
                <asp:Label ID="lblFilePath" Visible="false" runat="server" Text='<%# Eval("FilePath") %>' />
<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),
                                 FilePath = f
    protected void btnDownload_Click(object sender, EventArgs e)
        var filePath = string.Empty;

        string downloadFileName = "";
        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");
                filePath = (row.FindControl("lblFilePath") as Label).Text;

                if (cb != null && cb.Checked)
                    zip.AddFile(Server.MapPath(filePath), "Files");

Can anyone see how I could get this to work? I would appreciate any help.



Jan 6, 2011 at 6:18 PM

James - a couple things for you.

1. There's an example in the DotNetZip source distribution that  zips up a file based on checkbox status, and then downloads that zipfile to the browser.  You may wanna check it out.  It does almost exactly what you are describing.

2. If I was debugging this, I would attach to the ASPNET process with the debugger and step through the code so I could see it clearly.  I'd also want to wrap the zip creation logic in a try..catch... to handle exceptions.   I might also use the Fiddler2 tool to examine the HTTP request and response on the wire, to make sure it looks like I expect. 

What you are trying to do should work.   Nothing in your code jumps out at me as obviously wrong.   It could very well be a logical error in your code - if you are getting a valid, correct zip file, but the problem is, the zip is empty, then ... maybe you are not adding the files you thought you were adding.  If on the other hand you are seeing an empty response, that is something different.   

Good luck to you.


Mar 1, 2012 at 8:03 PM

Anyone figure this out??  I am needing a very similar solution but in ASP.NET VB.


Mar 2, 2012 at 5:16 AM

I tested this code. the only problem is with this line

 zip.AddFile(Server.MapPath(filePath), "Files");

There is no need to use Server.MapPath as Filepath is already having a physical path so use like this:

zip.AddFile(filePath, "Files");

If this does not work then paste you snippet.

Please let me know in case of any concerns.