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