Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}


by using this code iam able to download the file which is uploaded from my system and iam not able to download the same file in another system ...........what to do.it is showing exception file path not found.
Posted
Updated 1-Feb-14 0:24am
v7
Comments
Karthik_Mahalingam 30-Jan-14 0:02am    
filePath is incomplete
N.RamaRaju 30-Jan-14 0:10am    
ok thank you its data base problem
Karthik_Mahalingam 30-Jan-14 0:23am    
:) welcome

Save File Name in the Database

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            Session["sid"] = e.CommandArgument.ToString();
            if (sconn.State == ConnectionState.Open)
            {
                sconn.Close();
            }
            sconn.Open();
            string qry = "Select  FIle from File_Table where Id='" + Session["sid"].ToString() + "' ";
            SqlCommand cmd = new SqlCommand(qry, sconn);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);

            string fName = dt.Rows[0][0].ToString();

            cmd.ExecuteNonQuery();
            sconn.Close();
            if (fName == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('No Download Link Found');", true);
            }
            else
            {
                Response.Write("<script>alert('" + fName + "')</script>");
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
                Response.TransmitFile(Server.MapPath("~/Files/" + fName));
                Response.End();
            }
        }
    }
 
Share this answer
 
v2
// This button click event is used to download files from gridview

       protected void lnkDownload_Click(object sender, EventArgs e)
       {
           LinkButton lnkbtn = sender as LinkButton;
           GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
           string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
           Response.ContentType = "image/jpg";
           Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
           Response.TransmitFile(Server.MapPath(filePath));
           Response.End();
 
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