当前位置:网站首页>. Solution to the problem of Chinese garbled code when net core reads files
. Solution to the problem of Chinese garbled code when net core reads files
2022-07-02 15:21:00 【Dotnet cross platform】
background
In use today core web api Upload txt The documentation was going well , But a test found that the Chinese content read was garbled , I wonder .
Out of experience , Immediately put the code Encoding.Default Change to Encoding.uft8, I can't find it . Then simply save the uploaded file as next , Specially selected with bom Of utf8 Options .
But it is still garbled . depressed .
problem
So under the instinctive Baidu , There are a lot of ways to find , But none of them can solve the problem .
unconsciously , Found a link , Found the official website as follows
https://docs.microsoft.com/zh-cn/dotnet/api/system.text.encoding.default?view=netcore-3.1#System_Text_Encoding_Default
After go in , Be suddenly enlightened . That's what it says , About Encoding.Default The definition of attribute is as follows .
.NET Framework Default properties in
stay Windows On the table .NET Framework in , Default Property always gets the active code page of the system and creates Encoding Its corresponding object . The active code page may be ANSI code page , These include ASCII Character set and other characters different from the code page . Because of all Default be based on ANSI The coding of the code page will lose data , So please consider Encoding.UTF8 Use encoding instead . stay U + 00 To U + 7F Within the scope of ,UTF-8 It's usually the same , But it can be done without losing ASCII Encode characters outside the range .
.NET Core Default properties on
stay .NET Core in , Default Property always returns UTF8Encoding . Running .NET Core All operating systems of the application (Windows、Linux and macOS) All of us support UTF-8.
terms of settlement
So,.NET Framework The default encoding in is GB2312, And in the .NET Core China does not support it. , If you want to use, you must first register the code
The code is as follows
var completeFilePath = Path.Combine(filePath, saveName);
var stream = formFile.OpenReadStream();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding encoding = Encoding.GetEncoding("gb2312");
using var reader = new StreamReader(stream, encoding);
var input = reader.ReadToEnd();
Here we are , Problem solving .
边栏推荐
猜你喜欢
随机推荐
C language exercises - (array)
学习使用php将时间戳转换为大写日期的方法代码示例
Why can't programmers who can only program become excellent developers?
How to test tidb with sysbench
forEach的错误用法,你都学废了吗
AtCoder Beginner Contest 254
18_Redis_Redis主从复制&&集群搭建
Dragonfly low code security tool platform development path
[noi simulation] Elis (greedy, simulation)
List set & UML diagram
Map introduction
工程师评测 | RK3568开发板上手测试
GeoServer offline map service construction and layer Publishing
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
Implementation of n queen in C language
05_ queue
MFC A对话框调用B对话框函数并传参
【C语言】详解指针的初阶和进阶以及注意点(1)
06_栈和队列转换
Kibana basic operation