locked
Can any one tell what's wrong with my code RRS feed

  • Question

  • User672081070 posted
    I am going to save a file with .txt extension to my sql. I declared the column name for that as FData and Datatype as Longblob I use the following code to insert the data in to mysql 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(); } But the output in my txt file is displayed as string.byte[] instead of the required can any one tell why and resolve this if possible
    Tuesday, April 5, 2011 7:16 AM

Answers

  • User-718146471 posted

    I reformatted your code for easier reading - remember to use the {} code button when posting code please :)

    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(); }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 5, 2011 7:24 AM