Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add encoding information to the response stream in ASP.NET?

I have following piece of code:

public void ProcessRequest (HttpContext context) 
{
    context.Response.ContentType = "text/rtf; charset=UTF-8";
    context.Response.Charset = "UTF-8";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.AddHeader("Content-disposition", "attachment;filename=lista_obecnosci.csv");
    context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
}

When I try to open generated csv file, I get following behavior:

  • In Notepad2 - everything is fine.
  • In Word - conversion wizard opens and asks to convert the text. It suggest UTF-8, which is somehow ok.
  • In Excel - I get real mess. None of those Polish characters can be displayed.

I wanted to write those special encoding-information characters in front of my string, i.e.

context.Response.Write((char)0xef);
context.Response.Write((char)0xbb);
context.Response.Write((char)0xbf);

but that won't do any good. The response stream is treating that as normal data and converts it to something different.

I'd appreciate help on this one.

like image 950
Greg Avatar asked Jun 17 '09 07:06

Greg


People also ask

What is encoding in c#?

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. In contrast, decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.

What encoding does C# use for strings?

Essentially, string uses the UTF-16 character encoding form.

What UTF-8 means?

UTF-8 (UCS Transformation Format 8) is the World Wide Web's most common character encoding. Each character is represented by one to four bytes. UTF-8 is backward-compatible with ASCII and can represent any standard Unicode character.

What is contentencoding in http?

Gets or sets the HTTP character set of the output stream. A Encoding object that contains information about the character set of the current response. Attempted to set ContentEncoding to null. The following example writes a human-readable description of the character set encoding to the output stream.

What is the default stream reader encoding in Visual Studio Code?

The default stream reader encoding is UTF-8. But the file content of resources loaded from disk may or may not be UTF-8 encoded and in fact all my resource files are using the default encoding (Windows 1252) which is Visual Studio's file default for code files including JS files.

Why is it so hard to encode a script in net?

Now encoding is tricky in .NET because most of the text based readers use UTF-8 by default which means MOST of the time it works correctly with most content even if the content read is in fact not UTF-8 encoded. I used this simple code to load the script:

How to stream a response in WebAPI?

Streaming a response in .NET Core WebApi 1 By default, you have to write to the stream using the Async methods. If you try to write with non-Async methods,... 2 On C 3 you can have your streaming controller method return nothing at all. If you try the same on F 4 , you’ll get the... More ...


2 Answers

I ran into the same problem, and this was my solution:

context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
like image 170
Collin K Avatar answered Oct 15 '22 11:10

Collin K


What you call "encoding-information" is actually a BOM. I suspect each of those "characters" is getting encoded separately. To write the BOM manually, you have to write it as three bytes, not three characters. I'm not familiar with the .NET I/O classes, but there should be a method available to you that takes a byte or byte[] parameter and writes them directly to the file.

By the way, the UTF-8 BOM is optional; in fact, its use is discouraged by the Unicode Consortium. If you don't have a specific reason for using it, save yourself some hassle and leave it out.

EDIT: I just remembered you can also write the actual BOM character, '\uFEFF', and let the encoder handle it:

context.Response.Write('\uFEFF');
like image 25
Alan Moore Avatar answered Oct 15 '22 09:10

Alan Moore