开发者

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

开发者 https://www.devze.com 2023-02-21 22:42 出处:网络
In my MySql i am having my data field as longblob i would like to get the content in that file so i have written my code as follows

In my MySql i am having my data field as longblob i would like to get the content in that file so i have written my code as follows

This is what i am doing before inserting

  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 = byt开发者_JAVA技巧es;
    InsertUpdateData(cmd);

//Get Data

private void download(DataTable dt)
{
    Byte[] bytes = (Byte[])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]["Name"].ToString());
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

But i am getting the content as system.string[] why it is happening can any one tell


if dt.Rows[0]["FData"] is coming as a string (it is just plain text), use

byte[] bytes = Encoding.UTF8.GetBytes(dt.Rows[0]["FData"]);

If the data is not plain text and binary stored as string (and not base64 encoded), you are in trouble my friend.


UPDATE

According to MySql documentation, it seems you should be using LONGBLOB and not LONGTEXT.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号