1

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?

0

1 Answer 1

7

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

AKA:

 Response.BinaryWrite(content);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.