C# Asp.NETMVC5FileResult始终使用ANSI编码下载文件

C# Asp.NETMVC5FileResult始终使用ANSI编码下载文件,c#,encoding,asp.net-mvc-5,fileresult,C#,Encoding,Asp.net Mvc 5,Fileresult,我有这部分代码 Response.Charset = _encodingcode; Response.AddHeader("Content-Encoding", _encodingcode); Response.HeaderEncoding = Encoding.GetEncoding(_encodingcode); Response.ContentEncoding = Encoding.GetEncoding(_encodingcode); Response.ContentType = mim

我有这部分代码

Response.Charset = _encodingcode;
Response.AddHeader("Content-Encoding", _encodingcode);
Response.HeaderEncoding = Encoding.GetEncoding(_encodingcode);
Response.ContentEncoding = Encoding.GetEncoding(_encodingcode);
Response.ContentType = mimeType;                     

return File(_filedata, mimeType, $"{id}{_extension}");

但下载文件时,记事本的编码始终为ANSI

记事本只接收_filedata字节,这些字节以第3个参数中指定名称的文件形式保存到磁盘,但既不接收mimeType,也不接收响应头或响应体的任何元素,这就是为什么指定编码无效的原因

通过在字节流的开头添加字节顺序标记(BOM),可以向记事本提示某些编码:

  • 对于UTF-8-0xEF、0xBB、0xBF
  • 对于UTF-16/32-0xFF,0xFE

除此之外,没有办法告诉记事本应该使用什么编码。

默认情况下,如果您在编码中提供了不正确的文本,则每个编码都是ANSI。 改变用途的正确方法

Context.Response.Charset = Encoding.UTF8.WebName;
如果需要utf8,请执行此操作

 Encoding encoding = Encoding.UTF8;
     StreamWriter writer = new StreamWriter("Encoding.html", false, encoding);

     writer.WriteLine("<html><head>");

     // Write charset attribute to the html file.
     // The value of charset is returned by the WebName property.
     writer.WriteLine("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=" +
                           encoding.WebName +"\">");

         writer.WriteLine("</head><body>");
     writer.WriteLine("<p>" + HttpUtility.HtmlEncode(encoding.EncodingName) + "</p>");
     writer.WriteLine("</body></html>");
     writer.Flush();
     writer.Close();
Encoding=Encoding.UTF8;
StreamWriter=newstreamwriter(“Encoding.html”,false,Encoding);
writer.WriteLine(“”);
//将字符集属性写入html文件。
//字符集的值由WebName属性返回。
writer.WriteLine(“”);
writer.WriteLine(“”);
writer.WriteLine(“”+HttpUtility.HtmlEncode(encoding.EncodingName)+“

”; writer.WriteLine(“”); writer.Flush(); writer.Close();
如果您查找,我已经尝试使用Response.Charset。仍然是记事本++为所有文件打开具有相同封套的文件查看任何链接是否有帮助@GOMUGOMUNUO必须具体说明受保护的内部虚拟FileContentResult文件(字节[]fileContents、字符串contentType、字符串fileDownloadName)的内容;