Emaborsa
Emaborsa

Reputation: 2880

Upload file with BOM encoding

I need to upload/create a file on an FTP server with UCS-2 LE BOM encoding. I'm using C#.

Based on Microsoft's documentation, I have to use UnicodeEncoding with bigEndian:false and byteOrderMark:true. Here is the code:

using (WebClient client = new WebClient())
{
    client.Encoding = new UnicodeEncoding(false, true);
    client.Credentials = myCredentials;
    client.UploadString(path, WebRequestMethods.Ftp.UploadFile, myCsvInString);
}

The created file on the FTP server actually has UCS-2 Little Endian. For test purposes, I tried to switch the byteOrderMark to false and I got the same result.

Why? What am I missing?

I know that I could add '\uFEFF' but why isn't it done automatically?

Upvotes: 0

Views: 330

Answers (1)

Codo
Codo

Reputation: 79033

The interface and description of UnicodeEncoding could be improved regarding the handling of the byte order mark. UnicodeEncoding has a byte order mark attribute but the only method (aside from Equals and GetHashCode) using it is GetPreamble. All other methods and in particular the core method GetBytes don't.

The idea is to ensure the byte order mark is only ever written at the beginning of a file. UnicodeEncoding has no knowledge of the context. So it's up to the caller to add the preamble if needed (ie. byte order mark).

Based on this concept, WebClient.UploadString cannot assume it's uploading a file. It might be some other Unicode content. So it doesn't add the preamble.

You will have to add the preamble yourself. UnicodeEncoding.GetPreamble will return it.

Upvotes: 1

Related Questions