Download Binary (Byte) Data from database as ZIP file using C# and VB.Net in ASP.Net

Last Reply 10 days ago By dharmendr

Posted 10 days ago

Hi

i want to download two binary files from database on button click.

for some reason files are not adding to zip file.

Please Help

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
try
{
    byte[] bytes;
    byte[] bytes_img;
    // byte[] bytes_tbl;
    string contentType;
    //string contentType_img,contentType_tbl;
    string filename_ipc, filename;
    txtDownload.Text = tvBookContent.SelectedNode.Text;
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        SqlCommand cmd1 = new SqlCommand("select tbn.Rawdata_img,tbn.Rawdata_tbl,tbn.Rawfilename_img,tbn.Rawfilename_tbl from tblBookNodes_IPC tbn inner join tblModule tbm on tbn.iModuleId=tbm.iModuleId where tbm.vcModuleTitle='" + txtDownload.Text.ToString() + "'", conn);
        conn.Open();
        //cmd.Connection = conn;
        //cmd.CommandType = CommandType.StoredProcedure;
        //cmd.CommandText= "sp_fetch";
        //cmd.Parameters.AddWithValue("@filename", DropDownList2.Text.ToString());
        using (SqlDataReader dr = cmd1.ExecuteReader())
        {
            dr.Read();
            bytes = (byte[])dr["Rawdata_img"];
            bytes_img = (byte[])dr["Rawdata_tbl"];
            contentType = ".htm";
            filename = dr["Rawfilename_img"].ToString();
            filename_ipc = dr["Rawfilename_tbl"].ToString();
            zip.AddFile(filename);
            zip.AddFile(filename_ipc);
        }
        conn.Close();
        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();

    }
}

catch (Exception ex)
{

    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please selct chapter to download');", true);
    // Response.Redirect("DataCollection.aspx");
    setScreen();
    MultiView1.SetActiveView(View3);
}

 

You are viewing reply posted by: dharmendr 10 days ago.
Posted 10 days ago Modified on 10 days ago

Hi suhaas121,

For adding binary data to zip file you need to use AddEntry method.

Check the below code.

C#

protected void Zip(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select tbn.Rawdata_img,tbn.Rawdata_tbl,tbn.Rawfilename_img,tbn.Rawfilename_tbl from tblBookNodes_IPC tbn inner join tblModule tbm on tbn.iModuleId=tbm.iModuleId where tbm.vcModuleTitle=@title";
        cmd.Parameters.AddWithValue("@title", txtDownload.Text.Trim());
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                byte[] bytes = (byte[])dr["Rawdata_img"];
                byte[] bytes_img = (byte[])dr["Rawdata_tbl"];
                string filename = dr["Rawfilename_img"].ToString();
                string filename_ipc = dr["Rawfilename_tbl"].ToString();
                zip.AddEntry(filename, bytes);
                zip.AddEntry(filename_ipc, bytes_img);
            }
        }
        conn.Close();
        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();
    }
}

VB.Net

Protected Sub Zip(ByVal sender As Object, ByVal e As EventArgs)
    Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
    Using zip As ZipFile = New ZipFile()
        zip.AlternateEncodingUsage = ZipOption.AsNecessary
        Dim cmd As SqlCommand = New SqlCommand()
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "select tbn.Rawdata_img,tbn.Rawdata_tbl,tbn.Rawfilename_img,tbn.Rawfilename_tbl from tblBookNodes_IPC tbn inner join tblModule tbm on tbn.iModuleId=tbm.iModuleId where tbm.vcModuleTitle=@title"
        cmd.Parameters.AddWithValue("@title", txtDownload.Text.Trim())
        Using dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                Dim bytes As Byte() = CType(dr("Rawdata_img"), Byte())
                Dim bytes_img As Byte() = CType(dr("Rawdata_tbl"), Byte())
                Dim filename As String = dr("Rawfilename_img").ToString()
                Dim filename_ipc As String = dr("Rawfilename_tbl").ToString()
                zip.AddEntry(filename, bytes)
                zip.AddEntry(filename_ipc, bytes_img)
            End While
        End Using

        conn.Close()
        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