DOWNLOAD ZIP FILE IN ASP.NET USING C#
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="download.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="lblImageName" runat="server" ></asp:Label>
<asp:Button ID="bt" runat="server" OnClick="bt_Click" Text="Save" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace download
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial
Catalog=master;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (Session["imgUpload"] == null && flie.HasFile)
{
string FileName = Path.GetFileName(flie.PostedFile.FileName);
flie.SaveAs(Server.MapPath("download/" + FileName));
string image1 = "download/" +
FileName.ToString();
Session["FileUpload1"] =
image1;
lblImageName.Text = image1;
}
else if (Session["FileUpload1"] != null && (!flie.HasFile))
{
}
}
protected void bt_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into download
values('"+lblImageName.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void Download(object sender, EventArgs e)
{
Response.ContentType
= "application/zip";
Response.AppendHeader("Content-Disposition","attachment; filename=Docs.zip");
Response.TransmitFile(Server.MapPath("~/image/3 tier Sample.zip"));
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment;
filename=3 tier Sample.zip");
Response.TransmitFile(Server.MapPath("~/download/3
tier Sample.zip"));
Response.End();
}
}
}