简体   繁体   English

下载 txt 文件显示 System.Byte[] 以及如何将其转换为字符串? C#

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

I currently have a form that allows to me to successfully upload a txt file into a SQL database.我目前有一个表单,可以让我成功地将 txt 文件上传到 SQL 数据库中。 Now I'm trying to download that text file (or any txt file) but I'm not sure how to properly convert bytes.现在我正在尝试下载该文本文件(或任何 txt 文件),但我不确定如何正确转换字节。 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.它将显示列名“解决方案文件”,当它显示行时,它的“System.Byte”我尝试使用 byte[] 或 BitConverter,但我不确定如何。

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

 }

here is an example of converting to and from a byte array (assuming UTF-8 enconding).这是一个与字节数组相互转换的示例(假设为 UTF-8 编码)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM