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