Dorababu Meka

Dorababu Meka

  • 228
  • 8.3k
  • 1.7m

Unable to get the Content of the text file saved in the Database(My-Sql)

Apr 5 2011 8:50 AM
I have written the following code to insert a text file and get data from My-Sql but i am not getting the required output.

I created my table as follows

ID int AutoIncrement
Fname varchar
FData LongBlob

My code

Collapse
string filePath = Server.MapPath("AchTemplates/genACH.txt"); 
string filename = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
string strQuery = "insert into tblFiles(FName,FData) values (@_FName, @_FData)";
MySqlCommand cmd = new MySqlCommand(strQuery);
cmd.Parameters.Add("@_FName", MySqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@_FData", MySqlDbType.LongBlob).Value = bytes;
InsertUpdateData(cmd);
// To get the data i used the following code
private void download(DataTable dt)
{
Byte[] bytes = (Byte[])dt.Rows[0]["FData"];
//byte[] bytes = Encoding.UTF8.GetBytes(Convert.ToChar(dt.Rows[0]["FData"]));
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ContentType = dt.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["FName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
So can any one tell what's wrong going

Answers (1)