Monday, 22 June 2015

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 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();
        }
}

   
  
           



















          

          
       
  




0 comments:

Post a Comment