converting byte array to string not working in c#

I have a column AttachmentFile with varbinary data in the Database. In c#, I'm storing it in a byte[] array. I need to display this content as a string to the user. I've tried a couple of different ways to convert this byte array to a string, but nothing seems to work.

Here is what I'm trying to do -

while (rdr.Read())
{
    string name = rdr["AttachmentFileName"].ToString();
    string mime = rdr["AttachmentMIMEType"].ToString();
    byte[] content = (byte[])rdr["AttachmentFile"];

    string contentStr = (???)ConvertToString(content);
    r.AddHeader("Content-Disposition", "attachment; filename=" + name);
    r.ContentType = mime;
    r.Write(contentStr);
}

Here's what I have tried so far -

string contentStr = Encoding.Default.GetString(content, 0, 10000);
string contentStr = Encoding.UTF8.GetString(content, 0, 10000);

content = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, content);
string contentStr = Encoding.UTF8.GetString(content, 0, 10000);

System.Text.Encoding enc = System.Text.Encoding.ASCII;
string contentStr = enc.GetString(content);

string contentStr = System.Convert.ToBase64String(content);

But none of the above have given me a legible string back. Any ideas?


It looks like you are looking for the BinaryWrite method on the Response object.

AKA:

 Response.BinaryWrite(content);
链接地址: http://www.djcxy.com/p/89832.html

上一篇: 输入发生错误

下一篇: 将字节数组转换为不在c#中工作的字符串