上传txt文件编码格式判断(文本乱码解决方法)
说明
通过ajax或者浏览上传文本文件,上传时候c#处理时候因为文本格式的创建不同,在获取内容时候会出现中文乱码。
解决方法
通过上传的文件流,判断文件的编码格式,在使用对应的编码格式获取文本内容
#region 通过给定的文件流,判断文件的编码类型
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name=“fs“>文件流</param>
/// <returns>文件的编码类型</returns>
private string GetType(byte[] fs,out string errorMsg)
{
string reVal = "Default";
errorMsg = "";
if (IsUTF8Bytes(fs,out errorMsg) || (fs[0] == 0xEF && fs[1] == 0xBB && fs[2] == 0xBF))
{
reVal = "UTF8";
}
else if (fs[0] == 0xFE && fs[1] == 0xFF && fs[2] == 0x00)
{
reVal = "BigEndianUnicode";
}
else if (fs[0] == 0xFF && fs[1] == 0xFE && fs[2] == 0x41)
{
reVal = "Unicode";
}
return reVal;
}
#endregion
//获取文本内容
System.Text.Encoding.Default.GetString(fs)
测试结果
Windows 平台下Default(默认格式),UTF8,BigEndianUnicode,Unicode大部分能识别到
上传txt文件编码格式判断(文本乱码解决方法)的更多相关文章
- WordPress中"无法将上传的文件移动至"错误的解决方法
#chown -R www:www /wwwroot 今天在网页上传图片到博客,结果提示:“无法将上传的文件移动至 /home/wwwroot/wp-content/uploads/2013/”,郁闷 ...
- http上传txt文件
http通过post方式上传txt文件 可以参考 https://www.cnblogs.com/s0611163/p/4071210.html /// <summary> /// Htt ...
- Java中FTPClient上传中文目录、中文文件名乱码问题解决方法【好用】
转: Java中FTPClient上传中文目录.中文文件名乱码问题解决方法 问题描述: 使用org.apache.commons.net.ftp.FTPClient创建中文目录.上传中文文件名时,目录 ...
- php如何上传txt文件,并且读取txt文件
1.创建目录如下
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
在使用下面这个写法时候UTF-8文件编码 在读取时候出现乱码问题. File myFile=new File("文件路径"); BufferedReader in = new Bu ...
- 使用AndroidStudio上传忽略文件至SVN Server的解决措施
在同组项目进行共享时,容易把本地的配置文件比如*.iml等文件上传至共享服务器,这样会对队友造成巨大的麻烦,为了解决这个问题,可以使用下面方法解决,下面以上传到服务器的app.iml文件为例. 一.在 ...
- MultipartFile 获取上传TXT文件字数
@ResponseBody @RequestMapping(value = "/addImgForDynamic")//(发布动态) public Map addImgForDyn ...
- chrome文件上传 /获取文件路径c:/fakepath的解决办法
jsp页面 <td style="text-align: left;padding-left: 20px;"> <img name="image&quo ...
- Xcode7打包上传App Store发生ERROR ITMS-90535 错误解决方法
两个方法解决 1.如果用不到QQ分享啥的,直接删掉TencentOpenApi这个文件夹. 移除掉这个模块 ..简单粗暴,我就是这么解决的. 2.找到TencentOpenApi_IOS_Bundl ...
随机推荐
- spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
- WinExec可能会引起消息重入
WinExec不仅会造成延迟,并且还会引起消息的重入. 以下是调用堆栈: WinvoiceCC.exe!CWinvoiceCCDlg::OnMsgHttpReq(unsigned int wParam ...
- golang 中可变参数的个数
package main import "fmt" func Greeting(prefix string, who ... string) { fmt.Println(prefi ...
- 手动加入SSH支持、使用c3p0
之前做的笔记,如今整理一下.大家有耐心的跟着做就能成功: SSH(struts2.spring.hibernate) * struts2 * 充当mvc的角色 * hibernate ...
- [Python] How to unpack and pack collection in Python?
It is a pity that i can not add the video here. As a result, i offer the link as below: How to unpa ...
- javascript遍历数组的两种方法
var array=new Array(); array [0]="北京"; array [1]="天津"; ...//solution 1 for(var i ...
- iOS 特定图片的button的旋转动画
近期做的东西中,要为一个有特定图片的button加入旋转动画,Demo代码例如以下: #import "ViewController.h" @interface ViewContr ...
- 2016/1/27 1, File 创建 删除 改名 换路径 2,输出流 不覆盖 换行输入 3,输入流
1, File 创建 删除 改名 换路径 package Stream; import java.io.File; import java.io.IOException; public cla ...
- HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 解决Eclipse alt+/不出来提示的问题
1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the proposal ...