Not working, For you reference the code I am using is below, this might help you to find the problem.
Thanks in advance.
protected void LinkButton1_Click(object sender, EventArgs e)
{
string str = "SELECT [ID] ,[FileName] ,[Extension] ,[Content] FROM [TestDB].[dbo].[TestTable] where ID = (Select Max(ID) from TestTable ) ";
using (SqlConnection cnn = new SqlConnection("Data Source=earth;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=sa"))
{
cnn.Open();
SqlCommand cmd = new SqlCommand(str, cnn);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string strExtenstion = dr[2].ToString();
byte[] bytFile = (byte[])dr[3];
Response.Clear();
Response.Buffer = true;
if (strExtenstion == ".doc" || strExtenstion == ".docx")
{
// Response.ContentType = "application/msword";
Response.ContentType = "application/octet-stream";
//Response.AddHeader("Content-Disposition", "inline; filename=report.aspx.doc");
Response.AddHeader("content-disposition", "inline;filename=tr.doc");
}
else if (strExtenstion == ".xls" || strExtenstion == ".xlsx")
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=Tr.xls");
}
else if (strExtenstion == ".pdf")
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Tr.pdf");
}
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// If you write,
// Response.Write(bytFile1);
// then you will get only 13 byte in bytFile.
Response.BinaryWrite(bytFile);
Response.End();
}
}
}
Dont forget to click “Mark as Answer” on the post that helped you.
This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.