10

I don't understand what is missing here. I am trying to export a file in csv format with extended ASCII characters like ÿ or ü but all i get is �
Do I need to specify something else in the response?

Encoding encoding = Encoding.UTF8;

//ToCSV writes the string correctly
var bytes = encoding.GetBytes("write ÿ or ü please");
MemoryStream stream = new MemoryStream(bytes);

StreamReader reader = new StreamReader(stream);
//TextWriter tw = new TextWriter();
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.csv", fileName));
Response.Charset = encoding.EncodingName;
Response.ContentType = "application/text";
Response.Output.Write(reader.ReadToEnd());
Response.Flush();
Response.End();
3
  • Are you sure the client is using the right encoding to display the output? Commented Apr 8, 2011 at 3:43
  • I assume that by "all I get is �" you mean that is what you see in the browser. Have you examined the headers that your browser is receiving? Commented Apr 8, 2011 at 4:01
  • This code works fine for me Google Chrome as UA. Commented Apr 8, 2011 at 5:45

3 Answers 3

23

I believe you should add Response.ContentEncoding = Encoding.Unicode to get right output.

    Encoding encoding = Encoding.UTF8;
    var bytes = encoding.GetBytes("write ÿ or ü please");
    MemoryStream stream = new MemoryStream(bytes);
    StreamReader reader = new StreamReader(stream);
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.csv", "filename"));
    Response.Charset = encoding.EncodingName;
    Response.ContentType = "application/text";
    Response.ContentEncoding = Encoding.Unicode;
    Response.Output.Write(reader.ReadToEnd());
    Response.Flush();
    Response.End();
Sign up to request clarification or add additional context in comments.

2 Comments

It was actually specifying the charset by Response.Charset = encoding.BodyName;
encoding.WebName should be used here. BodyName gives incorrect value for some encodings. For example, "koi8-r" for "windows-1251" encoding.
6

Unfortunately Encoding.Unicode didn't work, using Windows-1252 worked :

Response.Clear();
Response.ContentType = "Application/x-msexcel";
Response.AddHeader("content-disposition", "attachment; filename=\"filename.csv\"");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252");
Response.Write(string.Join(Environment.NewLine, myDataLines));
Response.End();

Comments

0

I'm a Java programmer, are you sure you need a StreamReader? I guess StreamReader(stream) may decode the bytes in a different charset. And, You've already got the bytes, maybe you could write the bytes to response directly.

1 Comment

Maybe there is, but I am not sure how to do that. I will give it a try.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.