سؤال

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top