Apply this code in codebehind file for download image,document and any other file in zip folder.
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
DirectoryInfo dr = new DirectoryInfo(HttpContext.Current.Server.MapPath(strRequest));
// zip.AddDirectoryByName(strRequest);
if (dr.Exists)
{
foreach (FileInfo fl in dr.GetFiles())
{
string filePath = dr.FullName + "\\" + fl.Name;
//zip.AddFile(filePath, strRequest);
zip.AddFile(filePath,dr.Name);
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", dr.Name);
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.Flush();
Response.End();
}
else
{
// redirect to page which called download page
Response.Redirect(Server.MapPath(Request.UrlReferrer.AbsolutePath), false);
}
}
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
DirectoryInfo dr = new DirectoryInfo(HttpContext.Current.Server.MapPath(strRequest));
// zip.AddDirectoryByName(strRequest);
if (dr.Exists)
{
foreach (FileInfo fl in dr.GetFiles())
{
string filePath = dr.FullName + "\\" + fl.Name;
//zip.AddFile(filePath, strRequest);
zip.AddFile(filePath,dr.Name);
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", dr.Name);
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.Flush();
Response.End();
}
else
{
// redirect to page which called download page
Response.Redirect(Server.MapPath(Request.UrlReferrer.AbsolutePath), false);
}
}
No comments:
Post a Comment