How to conclude Stream.Write () in UTF-8 format

My problem is this:

I create and upload the SQL file using ASP.NET, but after saving the file to the FTP server, characters like ΓΌ change to & uul ;, ΓΈ to & oslash; and so on ... How can I prevent this? I do not want the file to be formatted using ASCII code, but with UTF-8.

The code that generates and downloads the file is as follows:

//request = the object to be made an request out of. Stream requestStream = request.GetReguestStream(); var encoding = new UTF8Encoding(); //fileContent is the string to be saved in the file byte[] buffer = encoding.GetBytes(fileContent); requestStream.Write(buffer, 0, buffer.Length); requestStream.Close(); 

As you can see, I tried using System.Text.UTF8Encoding , but it does not work.

+7
source share
2 answers

Remember that with threads, you can almost always wrap flows around as needed. If you want to write UTF-8 encoded content, you transfer the request stream to StreamWriter with the correct encoding:

 using (Stream requestStream = request.GetRequestStream()) using (StreamWriter writer = new StreamWriter(requestStream, Encoding.UTF8)) { writer.Write(fileContent); } 

Since you say you upload to the web service, be sure to set the content encoding. Since you did not send the request object there, I will consider it a normal HttpWebRequest .

Using HttpWebRequest, you tell the server what the encoding of content is using the ContentType property.

 request.ContentType = "text/plain;charset=utf-8"; 

As already mentioned, FTP transfer alone can break it. If you can, make sure it is transmitted in binary mode and not in ASCII mode.

+9
source

Put it in debugging and see what gets into the "buffer" after encoding. GetBytes () is called. This will check if it causes its rx side.

+1
source

All Articles