Download File As ZIP File From GridView
GridView Display Already Uploaded File Now Download Multiple File As ZIP FILE Using CheckBox Checked in Asp.Net C#.
Download Coding
Download
ZIP.Dll
DEMO
HTML CODING
C# CODING
GridView Display Already Uploaded File Now Download Multiple File As ZIP FILE Using CheckBox Checked in Asp.Net C#.
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:CheckBox ID="CheckBoxClick" runat="server" />
</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>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Download" />
</div>
</form>
</body>
</html>
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();
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBoxClick") as CheckBox).Checked)
{
string filename = (row.FindControl("lblFileName") as Label).Text;
string filePath = Server.MapPath("~/UploadFiles/" + filename);
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