如何确保我创建的文件下载是UTF-8? (而不是无 BOM 的 UTF-8)
我制作了一个下载功能,将消息下载到 CSV 文件(代码如下)。 现在,当我在记事本或记事本++中打开它时,我看到:
é NY ø ╬ ║► ░ ê ö
(顺便说一句,这就是数据库中的内容)
现在,当我在 Ms-Excel 中打开它时,它显示:
à © NY ╸ â•'â–º â–' ª ¶
当我在记事本++中打开它时,它说它是用“UTF8 without BOM”编码的。 当我将其(在记事本++中)编码为UTF-8时,一切顺利(也就是说,Excel也显示了正确的字符)
但是我如何确保我从代码创建的文件是UTF-8?< /strong>
这是我的代码:
public ActionResult DownloadPersonalMessages()
{
StringBuilder myCsv = new StringBuilder();
myCsv.Append(new DownloadService().GetPersonalMessages());
this.Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment; filename=PersonalMessages.csv");
Response.ContentEncoding = Encoding.UTF8;
Response.Write(myCsv.ToString());
Response.Flush();
Response.HeaderEncoding = Encoding.UTF8;
return Content("");
}
编辑:
我的函数现在返回一个带有此转换的ByteArray
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(str);
,我的下载现在是这样的:
Response.AddHeader("Content-Disposition", "attachment; filename=PersonalMessages.csv");
return File(new DownloadService().GetPersonalMessages(), "text/csv");
i've made a download function to download messages to a CSV file (code is below).
Now when i open it up in notepad or notepad++ i see this:
é NY ø ╬ ║► ░ ê ö
(and that is what is in the database btw)
Now, when i open it up in Ms-Excel it shows this:
é NY ø ╬ ║► ░ ê ö
When i open it up in notepad++, it says it's encoded in 'UTF8 without BOM'.
When i encode it (in notepad++) to UTF-8, all goes well (that is, Excel shows the right chars too)
But how can i make sure that the file i create from my code is UTF-8?
This is my code:
public ActionResult DownloadPersonalMessages()
{
StringBuilder myCsv = new StringBuilder();
myCsv.Append(new DownloadService().GetPersonalMessages());
this.Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment; filename=PersonalMessages.csv");
Response.ContentEncoding = Encoding.UTF8;
Response.Write(myCsv.ToString());
Response.Flush();
Response.HeaderEncoding = Encoding.UTF8;
return Content("");
}
Edit:
my function now returns a ByteArray
with this conversion
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(str);
and my download is now this:
Response.AddHeader("Content-Disposition", "attachment; filename=PersonalMessages.csv");
return File(new DownloadService().GetPersonalMessages(), "text/csv");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
泽雷的回答对OP有帮助,但实际上并没有回答问题。这是正确的解决方案,来自另一篇文章:
字节顺序标记(虽然技术上UTF8不需要)为某些程序提供线索(例如 Excel >2007)考虑到您正在使用 UTF8。您必须通过
GetPreamble()
方法手动包含它。Zareth's answer helped the OP, but it didn't actually answer the question. Here's the correct solution, from this other post:
The byte-order mark (while not technically required for UTF8) clues certain programs (e.g. Excel >2007) in to the fact that you're using UTF8. You have to manually include it via the
GetPreamble()
method.您可能想尝试使用 UTF8Encoding 类。构造函数有一个参数,用于确定是否应提供 BOM。您可能必须使用 GetBytes 方法并将字符串作为一系列字节写入响应中,而不是将其转换回 .net 字符串对象。
You might want to try using the UTF8Encoding class. The constructor has a parameter that determines if it should provide the BOM or not. You'll probably have to use the GetBytes-method and write the string as a series of bytes in the response, and not convert it back into a .net string object.
您可以稍微简化一下代码:
就 UTF-8 编码而言,恐怕问题可能出在这个
GetPersonalMessages
方法中。您可能需要返回一个流或字节数组,您可以直接将其作为文件返回。You could simplify your code a little:
As far as the UTF-8 encoding is concerned I am afraid the problem might be in this
GetPersonalMessages
method. You might need to return a stream or byte array which you could directly return as file.