In my ASP.NET webforms I have this function to download file, which I get from Database. Please how to change it to display this file in new browser window? It's mostly pdf or image.
protected void DownloadFile(object sender, EventArgs e)
{
string id = (sender as LinkButton).CommandArgument;
byte[] bytes;
string fileName;
//string contentType;
string constr = WebConfigurationManager.AppSettings["inputSqlCon"];
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = @"SELECT ATTACH_NAME, ATTACH
FROM PA_REQ_NOTE_ATTACHMENTS
where ATTACH_ID = @Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["ATTACH"];
//contentType = sdr["ContentType"].ToString();
fileName = sdr["ATTACH_NAME"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}