![](/img/trans.png)
[英]How to convert System.Byte[] into an Image to be Displayed into Word C#
[英]Downloading a txt file shows System.Byte[] and how to convert it to a string? c#
我目前有一个表单,可以让我成功地将 txt 文件上传到 SQL 数据库中。 现在我正在尝试下载该文本文件(或任何 txt 文件),但我不确定如何正确转换字节。 它将显示列名“解决方案文件”,当它显示行时,它的“System.Byte”我尝试使用 byte[] 或 BitConverter,但我不确定如何。
这是我的代码:
//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();
}
}
}
}
}
}
这是一个与字节数组相互转换的示例(假设为 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.