Downloading a txt file shows System.Byte[] and how to convert it to a string? c#

935 Views Asked by At

I currently have a form that allows to me to successfully upload a txt file into a SQL database. Now I'm trying to download that text file (or any txt file) but I'm not sure how to properly convert bytes. It will display the column name "Solution File" and when it shows the row its "System.Byte" I tried using byte[] or BitConverter but I am unsure how.

This is the result.

This is my code:

//this is the code for "Download"
protected void DownloadButton_Click(object sender, EventArgs e)
{
    string ConnectionStrings = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
    using (SqlConnection con = new SqlConnection(ConnectionStrings))
    {
        SqlCommand cmd = new SqlCommand("SELECT SolutionFile FROM dbo.acca_Problems where ProblemID = " + Request.QueryString["id"]);
        {
            
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
              using (DataTable dt = new DataTable())
                {           
                    sda.Fill(dt);
                    
                    //Build the Text file data.
                    string txt = string.Empty;

                    
                    foreach (DataColumn column in dt.Columns)
                    {
                         txt += column.ColumnName + "\r\n";
                        
                    }

                    //Add new line.
                    txt += "\r\n";

                    foreach (DataRow row in dt.Rows)
                    {
                        foreach (DataColumn column in dt.Columns)
                        {
                            
                            //Add the Data rows.
                            txt += row[column.ColumnName].ToString() + "\r\n";

                            
                        }

                        //Add new line.
                        txt += "\r\n";
                        //}

                        //Download the Text file.
                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=ProblemExport.txt");
                        Response.Charset = "";
                        Response.ContentType = "application/text";
                        Response.Output.Write(txt);
                        Response.Flush();
                        Response.End();
                    }
                }
            }
        }
    }

 }
1

There are 1 best solutions below

0
Eric Breyer On

here is an example of converting to and from a byte array (assuming UTF-8 enconding).

string toConvert = "Test string";

// From string to byte array
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(toConvert);

// From byte array to string
string s = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);