Download Single File As ZIP File From GridView Already Uploaded File Using in Asp.Net C#

Download File As ZIP File From GridView

GridView Display Already Uploaded File  Now Download Single File As ZIP FILE Using Download Button Clicked GridView Row in Asp.Net C#.
Multiple Zip Download
File Upload,Download
Download Single Zip File
Create Zip file



                 Download Coding
                              Download
                               ZIP.Dll

                                  DEMO



                            HTML CODING



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
         <div>
        <table><tr><td></td><td>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            </td></tr>
            <tr><td></td><td>
                <asp:Button ID="Button2" runat="server" Text="Upload" OnClick="Button2_Click" />
                </td></tr>
        </table>      
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" ForeColor="Blue"  Text="Download" OnClick="Button1_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="File Name">
                    <ItemTemplate>
                        <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("filename") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="filepath" HeaderText="File Path" />
            </Columns>
            <HeaderStyle ForeColor="#FF3300" />
        </asp:GridView>
        </div>
       
    </form>
</body>
</html>





                               C# CODING


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

public partial class ZipDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gridbind();        
        }    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new
        SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("UploadFiles/" + filename));
        SqlCommand cmd = new SqlCommand
("insert into reg(filename,filepath)values('" + filename + "','" + "UploadFiles/" + filename + "')", con);
        cmd.ExecuteNonQuery();
        Response.Write("<script>alert('File Uploaded')</script>");
        gridbind();
    }
    protected void gridbind()
    {
        SqlConnection con = new
        SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from reg", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();          
    }            
    protected void Button1_Click(object sender, EventArgs e)
       {
        ZipFile zip = new ZipFile();
        Button Btn = sender as Button;
        GridViewRow gvrow = Btn.NamingContainer as GridViewRow;
        Label filename = (Label)gvrow.FindControl("lblFileName") as Label;
        if (filename.Text != "")
        {
            string filePath = Server.MapPath("~/UploadFiles/" + filename.Text);
            zip.AddFile(filePath, "NewFolder");
        }

        Response.Clear();
        Response.BufferOutput = false;
        string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HHmmss"));
        Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment;fileName=" + zipName);
        zip.Save(Response.OutputStream);
        Response.End();
        }
}

   

First Create New Web Form -  Download Zip.Dll file - Add- Add Reference - Select Ionic.Zip.Dll File - Add
        
  
           



Next - Select - Add - Ionic.Zip.dll - Add






Next -  Select Added .dll File - Ok





Next - Added Bin Folder - Next  Create - Upload File  & Button





Next - Create Database Table - Add Filename & FilePath Fields.






Next - Add Gridview - EditColumn - Add Two Template Field & BoundField - Change Header & DataField 







Next - GridView - EditTemplateField - Add Button Control & Id






Next - Add  Label - Edit Dataitem - Bind Field Name - OK






Next - Add Ionic Namespaces Add & Insert Coding For Fiel Upload - Create Upload Folder






Next - Add Coding  Download Button  - Get Path Name - Set As DateTime Dynamically




          

          
       
  



0 comments:

Post a Comment