Hello,
I have the following code that opens a file saved in db as varbinary(MAX):
public void downloadFile(String appno, String file)
{
byte[] bytes;
SqlConnection con = getConnection();
try
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sproc";
cmd.Parameters.Add("@Appno", SqlDbType.Int).Value = appno == "" ? Convert.DBNull : appno;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr[file];
}
con.Close();
}
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
myMessageBox(ex.Message);
}
How can I open the file with the correct extension if I don't know the type? The user can upload images and PDF files. In the example I can only open pdf files.
Thank you in advance.