Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 5-Apr-11 3:10am
v5
Comments
Patrick Kalkman 5-Apr-11 8:05am    
Any tips about what exactly the code doesn't do, does it produce an error?
demouser743 5-Apr-11 8:08am    
While retrieving i am getting the data as System.string[] in my text file instead of the content

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

Regards
 
Share this answer
 
Comments
demouser743 5-Apr-11 9:04am    
Even this is also giving the same output :(
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:
C#
Response.AddHeader("content-disposition", "attachment;filename=" + (string)dt.Rows[0]["FName"]);
 
Share this answer
 
Comments
Ciumac Sergiu 5-Apr-11 9:53am    
dt.Rows[0]["FName"] - stands for the name of the file which should be Ok, as far as I understood the problem is within the data which is dt.Rows[0]["FData"].
Henry Minute 5-Apr-11 10:23am    
Sorry, my mistake.

Although if you are getting System.string[] you are almost certainly getting object.ToString(), even with your (Byte[]) cast.
Henry Minute 5-Apr-11 10:32am    
I had not looked carefully enough at your code and did not realize that your data was stored as a BLOB type.

The only way I know to read BLOB data is with a DataReader, as shown here --> http://bytes.com/topic/net/answers/768002-blob-mysql-using-c

Hope that helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900