Hi safzal.it,
Check this example. Now please take its reference and correct your code.
For creating zip file i have refered below article.
Database
I have made use of the following table with the schema as follows.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:HiddenField ID="hfId" runat="server" Value='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="File Name" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" Text="Download" runat="server" OnClick="Download" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.WebControls;
using Ionic.Zip;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.UI.WebControls
Imports Ionic.Zip
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Id,Name from tblFiles";
cmd.Connection = con;
con.Open();
gvFiles.DataSource = cmd.ExecuteReader();
gvFiles.DataBind();
con.Close();
}
}
}
}
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Download(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
foreach (GridViewRow row in gvFiles.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
string id = (row.FindControl("hfId") as HiddenField).Value;
byte[] bytes = GetBinaryData(id);
zip.AddEntry(row.Cells[1].Text.Trim(), bytes);
}
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
private byte[] GetBinaryData(string id)
{
byte[] bytes;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Data FROM tblFiles WHERE id = @Id";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
bytes = (byte[])cmd.ExecuteScalar();
con.Close();
}
return bytes;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "select Id,Name from tblFiles"
cmd.Connection = con
con.Open()
gvFiles.DataSource = cmd.ExecuteReader()
gvFiles.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim contentType As String = FileUpload1.PostedFile.ContentType
Using fs As Stream = FileUpload1.PostedFile.InputStream
Using br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", filename)
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
For Each row As GridViewRow In gvFiles.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
Dim id As String = (TryCast(row.FindControl("hfId"), HiddenField)).Value
Dim bytes As Byte() = GetBinaryData(id)
zip.AddEntry(row.Cells(1).Text.Trim(), bytes)
End If
Next
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.[End]()
End Using
End Sub
Private Function GetBinaryData(ByVal id As String) As Byte()
Dim bytes As Byte()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Data FROM tblFiles WHERE id = @Id"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
bytes = CType(cmd.ExecuteScalar(), Byte())
con.Close()
End Using
Return bytes
End Function
Screenshot