Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am using asp.net web application 2005

i have sql2005 binary field and stored in .doc, .pdf, .xls....files

i would like to open/download those files.

give me a total example.

Thanking you
Posted
Updated 21-Aug-12 20:30pm
v2
Comments
Member 8715996 22-Aug-12 2:29am    
..

Please check my solution in the below link.
I have explained how to upload and download files.i have explained table structure also for executing my solution.
http://www.c-sharpcorner.com/Forums/Thread/179462/how-to-retraive-pdf-file-from-sql-server-please-give-pro.aspx[^]
 
Share this answer
 
Comments
Member 8715996 23-Aug-12 1:10am    
Thanks Mr.Santhosh Kumar. Now i am going through that. after that i come back.
Ref:
ContentType string so that it specifies the appropriate file format. The syntax of this string is usually formatted as "type/subtype," where "type" is the general content category and "subtype" is the specific content type. For a full list of supported content types, refer to your Web browser documentation or the current HTTP specification. The following list outlines some common ContentType values:
"text/HTML"
"image/GIF"
"image/JPEG"
"text/plain"
"Application/msword" (for Microsoft Word files)
"Application/x-msexcel" (for Microsoft Excel files)

C#
public DownloadContent(string ID)
{
  SqlConnection con;
   try
        {

                con = new SqlConnection();
                con.ConnectionString = "YourConn";
                con.Open();
    
    
            string query = "your query to retrieve from db where ID = ID"// assuming 1 record here";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            
            if (dr.Read())
            {
                byte[] imagecontent = (byte[])(dr[1]); //assuming second record on dr is the file bytes
                Response.ContentType = "Application/msword"//sent your content type here, see ref link
                Response.AddHeader("Content-Disposition", "attachment;filename=somename.doc");//set file type here.
                Context.Response.BinaryWrite(imagecontent);
    
    
            }
            cmd = null;
            dr.Close();
            con.Close();
        }
        catch (Exception ex)
        {
        }
       finally
       {
        if(con!= null)
         con.Close();
       }
}
 
Share this answer
 
C#
try
        {
            DataSet ds1 = new DataSet();
            GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);


            //ReportID = Convert.ToInt32(gvAssesReport.DataKeys[gvr.RowIndex].Values[1]);
            FileName = gvReport.DataKeys[gvr.RowIndex].Values[0].ToString();
            int ReportID = Convert.ToInt16(gvReport.DataKeys[gvr.RowIndex].Values[1].ToString());
            if (string.IsNullOrEmpty(FileName))
            {
                lblMessage.Text = "File not found";
            }
            else
            {
                if (File.Exists(Server.MapPath(Constants.Report_SavePath.ToString() + "//" + ReportID + "//" + FileName)))
                {
                    Response.Clear();
                    Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
                    Response.ContentType = "application/Ms-Word";
                    lblMessage.Text = string.Empty;
                    Response.TransmitFile(Server.MapPath(Constants.Report_DisplayPath.ToString() + "//" + ReportID + "//" + FileName));
                    Response.Flush();
                }
                else
                {
                    lblMessage.Text = "File not found";
                }
            }
        }
        catch (Exception ex)
        {


        }
        finally
        {
            Response.End();
        }
 
Share this answer
 
v2

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