Friday, 1 October 2021

Zip file & download in Asp.Net

using Ionic.Zip;
using System.IO;
 
protected void btnDownload_Click(object sender, EventArgs e)
{
    try
    {
        if (HttpContext.Current.Session != null)
            ViewState["UniqueIdentifier"] = HttpContext.Current.Session.SessionID;
        else
            ViewState["UniqueIdentifier"] = Convert.ToString(Guid.NewGuid());
 
        string fileSavePath = Server.MapPath("Reports\\" + Convert.ToString(ViewState["UniqueIdentifier"]) + "\\SalesReport\\");
 
        if (!Directory.Exists(fileSavePath))
            Directory.CreateDirectory(fileSavePath);
 
        //Save file logic goes here
 
        ZipFileAndDownload();
    }
    catch (Exception ex) { }
}
 
protected void ZipFileAndDownload()
{
    try
    {
        string path = Server.MapPath("~/Reports/" + Convert.ToString(ViewState["UniqueIdentifier"]) + "/");
        if (Directory.Exists(path))
        {
            Response.Clear();
            Response.ClearContent();
            Response.Buffer = true;
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=Reports.zip");
            using (ZipFile zipfile = new ZipFile())
            {
                zipfile.AddSelectedFiles("*.*", path, "", true);
                zipfile.Save(Response.OutputStream);
            }
 
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
    catch (Exception ex) { }
    finally { DeleteExistingFile();}
}
 
protected void DeleteExistingFile()
{
    try
    {
        string path = Server.MapPath("Reports\\" + Convert.ToString(ViewState["UniqueIdentifier"]) + "\\");
        if (Directory.Exists(path))
        {
            string[] files = Directory.GetFiles(path);
            foreach (string item in files)
            {
                if (File.Exists(item))
                    File.Delete(item);
            }
            string[] directories = Directory.GetDirectories(path);
            foreach (string dir in directories)
                LoadSubDirectory(dir);
 
            // Delete sub folder of UniqueIdentifier folder
            foreach (string subfolders in Directory.GetDirectories(path))
                Directory.Delete(subfolders, true);
 
            // Delete main UniqueIdentifier folder
            Directory.Delete(path, true);
        }
    }
    catch (Exception ex) { }
}
 
protected void LoadSubDirectory(string directory)
{
    string[] files = Directory.GetFiles(directory);
    foreach (string item in files)
    {
        if (File.Exists(item))
            File.Delete(item);
    }
    string[] directories = Directory.GetDirectories(directory);
    foreach (string dir in directories)
    {
        LoadSubDirectory(dir);
    }

}


Note:- Add Ionic.Zip DLL in the project using NuGet Manager

No comments:

Post a Comment