![](/img/trans.png)
[英]Download file of any type in Asp.Net MVC using FileResult?
[英]Asp.Net MVC 5 FileResult always download file with ANSI encoding
我有這部分代碼
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)來向記事本提供有關某些編碼的提示:
除此之外,沒有辦法告訴記事本它應該使用什么編碼。
默認情況下,如果您在編碼中提供不正確的文本,則每個編碼都是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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.