如何在 C# 中使用带有 BOM 的 UTF8 编码 GetBytes()? [英] How to GetBytes() in C# with UTF8 encoding with BOM?
问题描述
我在 C# 中的 asp.net mvc 2 应用程序中遇到了 UTF8 编码问题.我正在尝试让用户从字符串下载一个简单的文本文件.我正在尝试使用以下行获取字节数组:
I'm having a problem with UTF8 encoding in my asp.net mvc 2 application in C#. I'm trying let user download a simple text file from a string. I am trying to get bytes array with the following line:
var x = Encoding.UTF8.GetBytes(csvString);
但是当我返回下载时使用:
but when I return it for download using:
return File(x, ..., ...);
我得到一个没有 BOM 的文件,所以我没有正确显示克罗地亚语字符.这是因为我的字节数组在编码后不包含 BOM.我尝试手动插入这些字节,然后它会正确显示,但这不是最好的方法.
I get a file which is without BOM so I don't get Croatian characters shown up correctly. This is because my bytes array does not include BOM after encoding. I triend inserting those bytes manually and then it shows up correctly, but that's not the best way to do it.
我还尝试创建 UTF8Encoding 类实例并将布尔值 (true) 传递给其构造函数以包含 BOM,但它也不起作用.
I also tried creating UTF8Encoding class instance and passing a boolean value (true) to its constructor to include BOM, but it doesn't work either.
有人有解决办法吗?谢谢!
Anyone has a solution? Thanks!
推荐答案
试试这个:
public ActionResult Download()
{
var data = Encoding.UTF8.GetBytes("some data");
var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
return File(result, "application/csv", "foo.csv");
}
原因是采用布尔参数的 UTF8Encoding 构造函数没有达到您的预期:
The reason is that the UTF8Encoding constructor that takes a boolean parameter doesn't do what you would expect:
byte[] bytes = new UTF8Encoding(true).GetBytes("a");
结果数组将包含一个值为 97 的字节.没有 BOM,因为 UTF8 不需要 BOM.
The resulting array would contain a single byte with the value of 97. There's no BOM because UTF8 doesn't require a BOM.
这篇关于如何在 C# 中使用带有 BOM 的 UTF8 编码 GetBytes()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!