[
  {
    "Id": "544616",
    "ThreadId": "240647",
    "Html": "\r\n<p>Hi,</p>\r\n<p>I am fairly new to Asp.net (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\r\n is the code I have so far, but the zip file it creates appears to be empty.</p>\r\n<p>GridView Markup:</p>\r\n<p></p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">GridView</span> <span style=\"color:red\">ID</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;gvFiles&quot;</span> <span style=\"color:red\">runat</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;server&quot;</span> <span style=\"color:red\">AutoGenerateColumns</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;false&quot;</span><span style=\"color:blue\">&gt;</span>\r\n    <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">Columns</span><span style=\"color:blue\">&gt;</span>\r\n        <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">TemplateField</span><span style=\"color:blue\">&gt;</span>\r\n            <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">ItemTemplate</span><span style=\"color:blue\">&gt;</span>\r\n                <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">CheckBox</span> <span style=\"color:red\">ID</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;chkSelect&quot;</span> <span style=\"color:red\">runat</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;server&quot;</span> <span style=\"color:blue\">/&gt;</span>\r\n            <span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">ItemTemplate</span><span style=\"color:blue\">&gt;</span>\r\n        <span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">TemplateField</span><span style=\"color:blue\">&gt;</span>\r\n        <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">TemplateField</span> <span style=\"color:red\">HeaderText</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;File Name&quot;</span><span style=\"color:blue\">&gt;</span>\r\n            <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">ItemTemplate</span><span style=\"color:blue\">&gt;</span>\r\n                <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">Label</span> <span style=\"color:red\">ID</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;lblFileName&quot;</span> <span style=\"color:red\">runat</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;server&quot;</span> <span style=\"color:red\">Text</span><span style=\"color:blue\">=</span><span style=\"color:blue\">'&lt;%# Eval(&quot;FileName&quot;) %&gt;'</span> <span style=\"color:blue\">/&gt;</span>\r\n                <span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">Label</span> <span style=\"color:red\">ID</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;lblFilePath&quot;</span> <span style=\"color:red\">Visible</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;false&quot;</span> <span style=\"color:red\">runat</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;server&quot;</span> <span style=\"color:red\">Text</span><span style=\"color:blue\">=</span><span style=\"color:blue\">'&lt;%# Eval(&quot;FilePath&quot;) %&gt;'</span> <span style=\"color:blue\">/&gt;</span>\r\n            <span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">ItemTemplate</span><span style=\"color:blue\">&gt;</span>\r\n        <span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">TemplateField</span><span style=\"color:blue\">&gt;</span>\r\n    <span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">Columns</span><span style=\"color:blue\">&gt;</span>\r\n<span style=\"color:blue\">&lt;/</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">GridView</span><span style=\"color:blue\">&gt;</span>\r\n<span style=\"color:blue\">&lt;</span><span style=\"color:#a31515\">asp</span><span style=\"color:blue\">:</span><span style=\"color:#a31515\">Button</span> <span style=\"color:red\">ID</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;btnDownload&quot;</span> <span style=\"color:red\">runat</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;server&quot;</span> <span style=\"color:red\">Text</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;Download&quot;</span> \r\n    <span style=\"color:red\">onclick</span><span style=\"color:blue\">=</span><span style=\"color:blue\">&quot;btnDownload_Click&quot;</span> <span style=\"color:blue\">/&gt;</span>\r\n<br>C# Code-Behind:</pre>\r\n</div>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">using</span> System;\r\n<span style=\"color:blue\">using</span> System.Text;\r\n<span style=\"color:blue\">using</span> System.IO;\r\n<span style=\"color:blue\">using</span> System.Collections.Generic;\r\n<span style=\"color:blue\">using</span> System.Linq;\r\n<span style=\"color:blue\">using</span> System.Web;\r\n<span style=\"color:blue\">using</span> System.Web.UI;\r\n<span style=\"color:blue\">using</span> System.Web.UI.WebControls;\r\n<span style=\"color:blue\">using</span> Ionic.Zip;\r\n\r\n<span style=\"color:blue\">public</span> <span style=\"color:blue\">partial</span> <span style=\"color:blue\">class</span> DownloadFiles : System.Web.UI.Page\r\n{\r\n    <span style=\"color:blue\">protected</span> <span style=\"color:blue\">void</span> Page_Load(<span style=\"color:blue\">object</span> sender, EventArgs e)\r\n    {\r\n        <span style=\"color:blue\">var</span> files = Directory.GetFiles(Server.MapPath(<span style=\"color:#a31515\">&quot;~/Uploads/Folder3&quot;</span>));\r\n\r\n        gvFiles.DataSource = <span style=\"color:blue\">from</span> f <span style=\"color:blue\">in</span> files\r\n                             <span style=\"color:blue\">select</span> <span style=\"color:blue\">new</span>\r\n                             {\r\n                                 FileName = Path.GetFileName(f),\r\n                                 FilePath = f\r\n                             };\r\n        gvFiles.DataBind();\r\n    }\r\n    <span style=\"color:blue\">protected</span> <span style=\"color:blue\">void</span> btnDownload_Click(<span style=\"color:blue\">object</span> sender, EventArgs e)\r\n    {\r\n        <span style=\"color:blue\">var</span> filePath = <span style=\"color:blue\">string</span>.Empty;\r\n\r\n        <span style=\"color:blue\">string</span> downloadFileName = <span style=\"color:#a31515\">&quot;Files.zip&quot;</span>;\r\n        Response.ContentType = <span style=\"color:#a31515\">&quot;application/zip&quot;</span>;\r\n        Response.AddHeader(<span style=\"color:#a31515\">&quot;content-disposition&quot;</span>, <span style=\"color:#a31515\">&quot;filename=&quot;</span> &#43; downloadFileName);\r\n\r\n        <span style=\"color:blue\">using</span> (ZipFile zip = <span style=\"color:blue\">new</span> ZipFile())\r\n        {\r\n            <span style=\"color:blue\">foreach</span> (GridViewRow row <span style=\"color:blue\">in</span> gvFiles.Rows)\r\n            {\r\n                CheckBox cb = (CheckBox)row.FindControl(<span style=\"color:#a31515\">&quot;chkSelect&quot;</span>);\r\n                filePath = (row.FindControl(<span style=\"color:#a31515\">&quot;lblFilePath&quot;</span>) <span style=\"color:blue\">as</span> Label).Text;\r\n\r\n                <span style=\"color:blue\">if</span> (cb != <span style=\"color:blue\">null</span> &amp;&amp; cb.Checked)\r\n                {\r\n                    zip.AddFile(Server.MapPath(filePath), <span style=\"color:#a31515\">&quot;Files&quot;</span>);\r\n                }\r\n            }\r\n            zip.Save(Response.OutputStream);\r\n        }\r\n    }\r\n}\r\n<br>Can anyone see how I could get this to work? I would appreciate any help.<br><br>Thanks,<br><br><br>James</pre>\r\n</div>\r\n<p></p>\r\n",
    "PostedDate": "2011-01-05T17:56:32.567-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "544999",
    "ThreadId": "240647",
    "Html": "\r\n<p>James - a couple things for you.</p>\r\n<p>1. There's an example in the DotNetZip source distribution that &nbsp;zips up a file based on checkbox status, and then downloads that zipfile to the browser.&nbsp; You may wanna check it out.&nbsp; It does almost exactly what you are describing.</p>\r\n<p>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.&nbsp; I'd also want to wrap the zip creation logic in a try..catch... to handle exceptions.&nbsp;&nbsp; I might also use\r\n the Fiddler2 tool to examine the HTTP request and response on the wire, to make sure it looks like I expect.&nbsp;</p>\r\n<p>What you are trying to do should work.&nbsp;&nbsp; Nothing in your code jumps out at me as obviously wrong.&nbsp; &nbsp;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,\r\n then ... maybe you are not adding the files you thought you were adding.&nbsp; If on the other hand you are seeing an empty response, that is something different.&nbsp; &nbsp;</p>\r\n<p>Good luck to you.</p>\r\n<p>-Cheeso</p>\r\n",
    "PostedDate": "2011-01-06T11:18:22.387-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "803986",
    "ThreadId": "240647",
    "Html": "<p>Anyone figure this out?? &nbsp;I am needing a very similar solution but in ASP.NET VB.</p>\r\n<p>Thanks,<br />Andrew</p>",
    "PostedDate": "2012-03-01T13:03:14.813-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "804180",
    "ThreadId": "240647",
    "Html": "<p>I tested this code. the only problem is with this line</p>\r\n<pre><span style=\"color: blue;\"></span> zip.AddFile(Server.MapPath(filePath), <span style=\"color: #a31515;\">\"Files\"</span>);<br /><br />There is no need to use Server.MapPath as Filepath is already having a physical path so use like this:<br /><br /><pre>zip.AddFile(filePath, <span style=\"color: #a31515;\">\"Files\"</span>);<br /><br />If this does not work then paste you snippet.<br /><br />Please let me know in case of any concerns.</pre>\r\n</pre>",
    "PostedDate": "2012-03-01T22:16:02.337-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]