Tuesday, 17 September 2013

How to Download Multiple Files as Zip File From Gridview Based on CheckBox Selection in Asp.Net Using C#





Description:-
            In Previous Example we Explain that How to Extract or Covert All Files to Zip Folder.Here we Explain that How to Download Multiple Selected File as a Zip File File From Gridview Based on CheckBox Selection in Asp.Net Using C#.
We All know that user have many thought like Download multiple File at a time and also selected Files is Download or other not. So to provide this type of Facility we can use this Example.
First using this Example we must have Download Ionic.Zip.dll from DotnetZIP Library.once download this File then you have to Add this dll file in Bin Folder of your Application.this dll are contain some code that are used to create a Zip File and just have to implement in our project.
below Image Descibe that How to add dll File in Bin Folder like
Now Create another Folder with name SampleFiles and this Folder are used for storing all uploaded file.


Before use this Example you must have to Add the Following Namespace in your Application like 

            using System.IO;
using Ionic.Zip;

To DownLoad Extract UnZip File into Zip Folder then click Here Unzip to zip File


To Download Complete Project plz Click below Download Image link
download here!


Default.aspx:-



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Download More than one Files as zip files in Asp.net using c#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fileUpload1" runat="server" />
    <asp:Button ID="btnupld" runat="server" Text="Upload more than one files" onclick="btnupld_Click" />   
    <br />
    <asp:Label ID="lblmsg" runat="server" Font-Bold="true" ForeColor="Red" />
    </div>
    <asp:GridView ID="gvDetails" CellPadding="4" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="chkchecked" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Text" HeaderText="uploadFileName" />
    </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
    <asp:Button ID="btndwnld" Text="Download All Checked Files" runat="server" onclick="btndwnld_Click" />
    </form>
</body>

</html>

 Default.aspx.cs:-


using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Ionic.Zip;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            Binddata();
        }
    }
    // Bind Data to Gridview
    protected void Binddata()
    {
        string[] dirPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string sourcepath in dirPath)
        {
            files.Add(new ListItem(Path.GetFileName(sourcepath)));
        }
        gvDetails.DataSource = files;
        gvDetails.DataBind();
    }
    // insert all the files in Destination Directory
    protected void btnupld_Click(object sender, EventArgs e)
    {
        if (fileUpload1.HasFile)
        {
            string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
            string path = Server.MapPath("~/SampleFiles/" + filename);
            fileUpload1.SaveAs(path);
            lblmsg.Text = "File was Successfully Uploaded";
            Binddata();
        }
    }  
    // convert all files to Zip  folder
    protected void btndwnld_Click(object sender, EventArgs e)
    {
        using (ZipFile zip = new ZipFile())
        {
            foreach (GridViewRow gvrow in gvDetails.Rows)
            {
                CheckBox chk = (CheckBox)gvrow.FindControl("chkchecked"); //Get all files list that are checked in Gridvieww
                if(chk.Checked)
                {
                    string filenm= gvrow.Cells[1].Text ;
                    string filePath = Server.MapPath("~/SampleFiles/" + filenm);
                    zip.AddFile(filePath, "files");
                }
            }
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip"); // name of .Zip File
            Response.ContentType = "application/zip";
            zip.Save(Response.OutputStream);
            Response.End();
        }
    }
  }


 

0 comments:

Post a Comment