We are storing file as BLOB in mysql with its filename also.While uploading its working fine. Now we want to download it fron gridview this also works perfect. But the problem is that after downloading the fine when we try to open it it shows file format error. We are only dealing with office document and pdf only.
try
{
int did = Convert.ToInt32(e.CommandArgument);
DataSet path = download.GetresourcePath(did);
byte[] fileFromDatabase = null;
DataRow row = path.Tables[0].Rows[0];
fileFromDatabase = (byte[])row["UPLFILE"];
string filename = (string)row["FILENAME"];
if (fileFromDatabase.Length > 0)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = string.Format("application/{0}",
Path.GetExtension(filename).Substring(1));
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}", filename));
Response.BinaryWrite(fileFromDatabase);
Response.Flush();
Response.End();
}
}
catch (Exception)
{
return;
}
- Here did is the ID of the file we getting it from the grid view.
- filename is the name of the file we are getting from the database.
- fileFromDatabase is the BLOB file from Mysql and we converted it into Byte.
So can any one suggest me what i am doing wrong?