How to make sure my created filedownload is UTF-8? (and not UTF-8 without BOM)

后端 未结 3 1393
悲&欢浪女
悲&欢浪女 2021-02-08 15:51

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 ø ╬ ║► ░ ê ö

(

3条回答
  •  醉酒成梦
    2021-02-08 16:37

    Zareth's answer helped the OP, but it didn't actually answer the question. Here's the correct solution, from this other post:

    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");
    }
    

    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.

提交回复
热议问题