Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of garbled Chinese characters when .net Core reads files

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to solve Chinese garbled when .net Core reads files". In daily operation, I believe many people have doubts about how to solve the problem of Chinese garbled when .net Core reads files. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to solve Chinese garbled when .net Core reads files". Next, please follow the editor to study!

Background

It was smooth to upload txt documents using core web api, but a test found that the read Chinese content was garbled, which was very puzzling.

Out of experience, I immediately changed the Encoding.Default of the code to Encoding.uft8, and found that it still didn't work. After that, simply save the uploaded file as a file, and specifically select the utf8 option with bom.

But I found it was still garbled. Depressed.

problem

So in the instinctive Baidu, found a lot of methods, but none of them can solve the problem.

After going in, it suddenly became clear. This is what it says, and the definition of the Encoding.Default property is like this.

Default properties in .NET Framework

In the .NET Framework on the Windows desktop, the Default property always gets the system's active code page and creates an object corresponding to the Encoding. The active code page may be an ANSI code page, which includes the ASCII character set and other characters that are different from the code page. Because all Default code page-based encodings based on ANSI will lose data, consider Encoding.UTF8 using encoding instead. UTF-8 is usually the same in the U + 00 to U + 7F range, but characters can be encoded outside the ASCII range without loss.

Default properties on .NET Core

In .NET Core, the Default property always returns UTF8Encoding. UTF-8 is supported on all operating systems (Windows, Linux, and macOS) running .NET Core applications.

Solution.

The default encoding in So,.NET Framework is GB2312, but it is not supported in .NET Core. If you want to use it, you must first register the encoding.

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 ()

At this point, the problem is solved.

Read the configuration file in Chinese garbled code under. Net Core

Net Core under the configuration file is json format, read the configuration file if we set Chinese will appear garbled, how to solve it. Json

There is a way to find a way on the Internet, as long as the appsetting.json is opened in notepad, and then saved as a format to select UTF-8, you can app.

Then there will be no garbled code when reading the Chinese information of the configuration file, blo.

At this point, the study of "how to solve Chinese garbled when .net Core reads files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report