lin*_*nkb 8 .net c# asp.net utf-8
我的问题是:
我生成并上传使用ASP.NET SQL文件,但该文件保存到FTP服务器后,像ü字符改为&多山,O到O和等等...我怎样才能防止这种情况发生?我不希望使用ASCII代码格式化文件,但使用UTF-8.
生成并上传文件的代码如下所示:
//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();
Run Code Online (Sandbox Code Playgroud)
正如你所看到我试图使用它System.Text.UTF8Encoding,但它不起作用.
Jos*_*hua 11
请记住,对于流,您几乎总是可以根据需要包裹流.如果要编写UTF-8编码的内容,请StreamWriter使用正确的编码将请求流包装在一起:
using (Stream requestStream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream, Encoding.UTF8)) {
writer.Write(fileContent);
}
Run Code Online (Sandbox Code Playgroud)
由于您说您要上传到网络服务,请务必同时设置内容编码.由于你还没有发布request对象的来源,我认为这是一个普通的HttpWebRequest.
使用HttpWebRequest,您可以使用该ContentType属性告诉服务器内容编码是什么.
request.ContentType = "text/plain;charset=utf-8";
Run Code Online (Sandbox Code Playgroud)
正如其他人所提到的,FTP传输本身也可能会破坏它.如果可以,请确保它以二进制模式而非ASCII模式传输.