Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# context.Response.OutputStream.Write Hebrew characters

Tags:

c#

I'm trying to export a report to csv using this code:

byte[] csvBytes = AttachmentFileAgent.GetAttachmentFilesCSVBytes(context.Request["appCode"], performingPerson);
context.Response.AddHeader("content-disposition", "attachment; filename=ApplicationReport_" + context.Request["appCode"] + ".csv");
context.Response.ContentType = "text/csv;charset=utf-8";
context.Response.Charset = "utf-8";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.OutputStream.Write(csvBytes, 0, csvBytes.Length);

But the Hebrew content goes out in gibberish (׳›׳¢׳›׳’׳¢׳›׳’׳›׳¢). I tryied ising all kinds on encoding (UTF8, Unicode, ASCII) but nothing works...

like image 524
Liran Friedman Avatar asked Nov 09 '22 13:11

Liran Friedman


1 Answers

Your changes to context.Response.ContentEncoding sets the encoding on the TextWriter used by context.Response.Output

When using context.Response.OutputStream you need to set the UTF-8 encoding used in your GetAttachmentFilesCSVBytesfunction.

Also see ASP.NET: Will Saving an XmlDocument to the Response.OutputStream honor the encoding?

like image 126
KCD Avatar answered Nov 15 '22 10:11

KCD