3

I have this part of code

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

But always when download the file the notepad's encoding is ANSI

Nkosi
  • 235,767
  • 35
  • 427
  • 472
GomuGomuNoRocket
  • 771
  • 2
  • 11
  • 37
  • 1
    [just check this discussion](https://stackoverflow.com/questions/25320674/asp-net-mvc-how-to-set-encoding-when-returning-fileresult) – Matt Qafouri Jul 20 '19 at 10:00
  • if you look up i have arleady tried to use Response.Charset. Still notepad++ open file with the same encding for all files – GomuGomuNoRocket Jul 22 '19 at 11:41
  • See if any of the links help? https://stackoverflow.com/questions/1679656/asp-net-excel-export-encoding-problem , https://stackoverflow.com/questions/1376602/%C3%AF-character-utf-8-bom-in-middle-of-asp-net-response-due-to-httpresponse-tran, https://github.com/SmallAnts/Smart/blob/a1e51221a690788aeb6f950b0f683c6cf5f2acc6/Src/Framework/Smart.Web.Mvc.Shared/ControllerBase.cs#L129 – Tarun Lalwani Jul 22 '19 at 13:46
  • @GomuGomuNoRocket be specific about what `_filedata` is – Nkosi Jul 22 '19 at 22:55
  • protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName); – GomuGomuNoRocket Jul 23 '19 at 07:56
  • @Tarun Lalwani i tried everything that you suggested. But ALWAYS AND ALWAYS the notepad open the file with ANSI as UTF-8 – GomuGomuNoRocket Jul 23 '19 at 08:36
  • Try with notepad++ instead of just notepad – Tarun Lalwani Jul 23 '19 at 09:58
  • You can add a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) to the start of a text file to explicitly define it as UTF-8. If you don't, software like Notepad simply don't explicitly know what encoding a text file is because text files dont have any structure to allow it to, it's simply an unstructured stream of bytes that's inferred to be one encoding or another by its contents. – Prime Jul 24 '19 at 21:34
  • @GomuGomuNoRocket, how do you set _filedata? – Jeric Cruz Jul 28 '19 at 04:23
  • Encoding encoding = Encoding.UTF8; HttpUtility.HtmlEncode(encoding.EncodingName) – Jin Thakur Aug 06 '19 at 16:20
  • your code is so incomplete what is the value of _encodingcode; Response.Charset = _encodingcode; – Jin Thakur Aug 06 '19 at 16:21
  • use this for utf8 Response.Charset = Encoding.UTF8.WebName; – Jin Thakur Aug 06 '19 at 16:26

2 Answers2

0

Notepad receives only _filedata bytes which are saved to disk as file with name specified in 3rd parameter, but neither mimeType nor any element of response header or body, that why specifying encoding has no effect.

You can give a hint to Notepad about some encodings by adding Byte Order Mark (BOM) at the beginning of you bytes stream:

  • For UTF-8 - 0xEF,0xBB,0xBF
  • For UTF-16/32 - 0xFF, 0xFE

Apart from that, there is no way to tell Notepad what encoding it should use.

pakeha_by
  • 2,081
  • 1
  • 15
  • 7
0

By default every encoding is ANSI if you provide improper text in encoding it goes as ANSI. Right way to change use

Context.Response.Charset = Encoding.UTF8.WebName;

If you want utf8 do this

 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();
Jin Thakur
  • 2,711
  • 18
  • 15