Click here to Skip to main content
16,017,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have stored the file details in a database and have successfully displayed the files details(name,upload date uploaded by etc) in a gridview.However i also have a download button(button type is link) which should be able to downlaod the file.The file paths are stored in the database whereas the files are stored in a directory.Can some how i use the files path from the Database to download files..any codes

Thanks
Posted
Comments
Kenneth Haugland 23-Aug-12 17:59pm    
As long as you have full accsess to the directory you coul djust use copy function, if its over the web its not so simple.

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())
            {
                string path =Convert.ToString(dr[1]); //assuming second record on dr is the file path
                byte[] content = System.IO.File.ReadAllBytes(path); //you may have to modify path using Server.MapPath -> depend the on the filepath you stored.
                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(content);
    
    
            }
            cmd = null;
            dr.Close();
            con.Close();
        }
        catch (Exception ex)
        {
        }
       finally
       {
        if(con!= null)
         con.Close();
       }
}
 
Share this answer
 
v2
I assume the missing bit of info is that this is a web app. Your IIS needs to be configured to serve the file type and you can replace the path to your web root with www.yourdomain.com to generate a URL to your file.
 
Share this answer
 

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