Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am doing web application .Here I want to download bulk of files which are stored in sql server in var-binary format and archive all in zip folder.I already create zip folder from Directory storage as follows...

What I have tried:

<pre> string path = Server.MapPath("~/Samples/");
            string[] filenames = Directory.GetFiles(path);          
            using (ZipFile zip = new ZipFile())
            {
                //Byte [] filename1=
                zip.AddFiles(list, "files");
                zip.Save(Server.MapPath("~/samplefiles1.zip"));
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Message", "alert('Zip File Created')", true);
            }


but now i want get pdf files from sql server and add to zip folder..I googled a lot but i can't .
Please help me..
thanks
Sujata
Posted
Updated 1-Feb-17 21:19pm
v2

1 solution

refer this article to save all the pdfs to the disc and then zip the files based on the ZipFile api
Save PDF File in SQL Server Database using C#[^]

similar question database to store for PDF,doc,pdf, and Images[^]
 
Share this answer
 
Comments
SujataJK 2-Feb-17 3:38am    
thanks,Karthik sir.
As per understanding above link save individual pdf to the disc.but i want simultaneously download or create zip folder of all pdf stoared in database
Karthik_Mahalingam 2-Feb-17 3:49am    
First save all the files to the disc
zip all the saved files
download the zip file using respose.write with zip content type
SujataJK 2-Feb-17 4:02am    
ok..
but how can do that? as per ur above link..
string sPathToSaveFileTo = @"C:\SelectedFile.pdf";
// on this path i will create selected PDF File Data open pdf for checking

this only for selected file but i want to perform download action on btnDownloadAll_Click().i.e.Download all file in single select query.
Karthik_Mahalingam 2-Feb-17 4:29am    
you should save the files in a folder inside your project directory.
SujataJK 2-Feb-17 4:05am    
Following code i used for download PDF when download linkButton of gridview click.
i.e by passing particular id

protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int fileid = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value.ToString());

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, ContentType, Data from ImageTable where id=@Id";
cmd.Parameters.AddWithValue("@Id", fileid);
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["ContentType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Name"] + "\"");
Response.BinaryWrite((byte[])dr["Data"]);
Response.End();
byteArray = (byte[])dr["Data"];
string result = System.Text.Encoding.UTF8.GetString(byteArray);
list.Add(result);

}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900