Hi goplaa,
Here i have created sample that fullfill your requirement.
Here i have used the below article to insert binary data to database.
HTML
<div>
<asp:DropDownList runat="server" ID="ddlCountries" OnSelectedIndexChanged="ddComapanyFilter_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="Test 1" Value="516" />
<asp:ListItem Text="Test 2" Value="517" />
<asp:ListItem Text="Test 3" Value="518" />
<asp:ListItem Text="Test 4" Value="519" />
</asp:DropDownList>
</div>
Code
protected void ddComapanyFilter_SelectedIndexChanged(object sender, EventArgs e)
{
string id = ddlCountries.SelectedItem.Value;
byte[] bytes = null;
string contentType = string.Empty;
string name = string.Empty;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM tblFiles WHERE Id = @Id", con);
cmd.Parameters.AddWithValue("@Id", id);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
bytes = (byte[])reader["Data"];
contentType = reader["ContentType"].ToString();
name = reader["Name"].ToString();
}
}
con.Close();
}
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
// If you want to open in browser then comment the below line of code "Response.AddHeader("content-disposition", "attachment;filename=" + name);".
Response.AddHeader("content-disposition", "attachment;filename=" + name);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
Screenshot
