I am trying to download only checked selected files from directory in zip format but in my code all the files in that directory is being downloaded inside zip file.
Can you tell me where i am wrong.
protected void btndownload_OnClick(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
foreach (GridViewRow row in grdShowData.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkselect");
if (chk.Checked)
{
try
{
string[] files = Directory.GetFiles(ConfigurationManager.AppSettings["EmployeeDocuments_Path"].ToString());
foreach (string file in files)
{
zip.AddFile(file, (row.FindControl("lblDocumentPath") as Label).Text);
}
}
catch (Exception ex)
{
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error!", "alert('File not Found');", true);
}
}
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("EmployeeDoc_" + ddlDocType.SelectedItem.ToString() + ".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();
}
}