Save File Name in the Database
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();
}
}
}