无法获取保存在数据库中的文本文件的内容(My-Sql) [英] Unable to get the Content of the text file saved in the Database(My-Sql)

查看:156
本文介绍了无法获取保存在数据库中的文本文件的内容(My-Sql)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来插入文本文件并从My-Sql获取数据,但未获得所需的输出.

我按如下方式创建了表格

ID int AutoIncrement
Fname varchar
FData LongBlob

我的代码

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

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?

推荐答案


Response.BinaryWrite仅用于写入非字符串信息,因此我宁愿先将byte[]转换为字符串,然后将其写入Response.
Hi,
Response.BinaryWrite is used merely for writing non-string information, so I''d rather convert the byte[] to a string first, and then write it to the Response.
string s = Encoding.UTF8.GetString((Byte[])dt.Rows[0]["FData"]);
Response.Write(s);


问候


当您获得类似System.string[]的输出时,这表明您要求输入单元格/字段的Type,这是因为dt.Rows[0]["FName"]返回的是object,默认情况下,在调用ToString()时返回其类型.

要获取内容,请尝试转换为字符串:
When you get output like System.string[] this indicates that you have asked for the Type of the cell/Field, this is because dt.Rows[0]["FName"] returns an object which, by default, returns it''s Type when ToString() is called on it.

To get the content try casting to a string:
Response.AddHeader("content-disposition", "attachment;filename=" + (string)dt.Rows[0]["FName"]);


这篇关于无法获取保存在数据库中的文本文件的内容(My-Sql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆