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

后端 未结 3 1384
悲&欢浪女
悲&欢浪女 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:43

    You could simplify your code a little:

    public ActionResult DownloadPersonalMessages()
    {
        StringBuilder myCsv = new StringBuilder();
        myCsv.Append(new DownloadService().GetPersonalMessages());
        Response.AddHeader("Content-Disposition", "attachment; filename=PersonalMessages.csv");
        return File(Encoding.UTF8.GetBytes(myCsv.ToString()), "text/csv");
    }
    

    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.

提交回复
热议问题