当前位置:网站首页>. 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 .
边栏推荐
- 4. Data splitting of Flink real-time project
- 20_Redis_哨兵模式
- 学习使用php实现公历农历转换的方法代码
- AtCoder Beginner Contest 254
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
- LeetCode 2320. Count the number of ways to place the house
- Data analysis thinking analysis methods and business knowledge - business indicators
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- 搭建自己的语义分割平台deeplabV3+
- 语义分割学习笔记(一)
猜你喜欢

Table responsive layout tips
![[noi simulation] Elis (greedy, simulation)](/img/a2/f8c8ab3bc8dd779327be3f76990976.png)
[noi simulation] Elis (greedy, simulation)

About text selection in web pages and counting the length of selected text

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

21_ Redis_ Analysis of redis cache penetration and avalanche

Dragonfly low code security tool platform development path

05_ queue

Btrace- (bytecode) dynamic tracking tool

Data analysis thinking analysis methods and business knowledge - business indicators

Solve the problem that El radio group cannot be edited after echo
随机推荐
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
14_Redis_乐观锁
Table responsive layout tips
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
TiDB 软件和硬件环境建议配置
13_ Redis_ affair
Base64 编码原来还可以这么理解
21_Redis_浅析Redis缓存穿透和雪崩
C language exercises - (array)
数据分析思维分析方法和业务知识——业务指标
06_栈和队列转换
TiDB跨数据中心部署拓扑
18_Redis_Redis主从复制&&集群搭建
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Huawei interview question: no palindrome string
yolo格式数据集处理(xml转txt)
Tidb environment and system configuration check
牛客练习赛101
TiDB数据迁移场景综述