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

Last Reply one month ago By dharmendr

Posted one month 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);
}

 

This question does not have replies that have been liked.