<el-upload
ref="upload2"
:auto-upload="false"
:show-file-list="false"
:on-change="handleFileUploaderChange2"
action="#"
accept=".xlsx, .xls, .txt"
class="avatar-uploader">
<el-button size="mini" type="text" icon="el-icon-upload">
{{ $t('programModel.importSource') }}</el-button>
</el-upload>
handleFileUploaderChange2(res, file) {
const self = this
const reader = new FileReader()
reader.readAsText(res.raw, 'gb2312') //读取内容并解决乱码的核心代码
reader.onload = function(event) {
self.ruleForm.content = reader.result
}
},
假如你的乱码还是没有解决,注意,上面我用的是gb2312这是windows默认的字符编码(中国),假如你想知道你的windows是什么编码的话
用命令行查看
可以自行对照
下表列出了所有支持的代码页及其国家(地区)或者语言:
代码页 国家(地区)或语言
437 美国
708 阿拉伯文(ASMO 708)
720 阿拉伯文(DOS)
850 多语言(拉丁文 I)
852 中欧(DOS) - 斯拉夫语(拉丁文 II)
855 西里尔文(俄语)
857 土耳其语
860 葡萄牙语
861 冰岛语
862 希伯来文(DOS)
863 加拿大 - 法语
865 日耳曼语
866 俄语 - 西里尔文(DOS)
869 现代希腊语
874 泰文(Windows)
932 日文(Shift-JIS)
936 中国 - 简体中文(GB2312)
949 韩文
950 繁体中文(Big5)
1200 Unicode
1201 Unicode (Big-Endian)
1250 中欧(Windows)
1251 西里尔文(Windows)
1252 西欧(Windows)
1253 希腊文(Windows)
1254 土耳其文(Windows)
1255 希伯来文(Windows)
1256 阿拉伯文(Windows)
1257 波罗的海文(Windows)
1258 越南文(Windows)
20866 西里尔文(KOI8-R)
21866 西里尔文(KOI8-U)
28592 中欧(ISO)
28593 拉丁文 3 (ISO)
28594 波罗的海文(ISO)
28595 西里尔文(ISO)
28596 阿拉伯文(ISO)
28597 希腊文(ISO)
28598 希伯来文(ISO-Visual)
38598 希伯来文(ISO-Logical)
50000 用户定义的
50001 自动选择
50220 日文(JIS)
50221 日文(JIS-允许一个字节的片假名)
50222 日文(JIS-允许一个字节的片假名 - SO/SI)
50225 韩文(ISO)
50932 日文(自动选择)
50949 韩文(自动选择)
51932 日文(EUC)
51949 韩文(EUC)
52936 简体中文(HZ)
65000 Unicode (UTF-7)
65001 Unicode (UTF-8)
所以如果用gb2312还是乱码的话,那就是你那个txt并不是用gb2312默认的中文编码写的,所以需要你用什么编码写的txt,就用什么编码来解。
handleFileUploaderChange2(res, file) {
const self = this
const reader = new FileReader()
reader.readAsText(res.raw, 'utf-8') //读取内容并解决乱码的核心代码
reader.onload = function(event) {
self.ruleForm.content = reader.result
}
},
如果是读取xls xlsx的内容的话,是需要借助插件才读内容的,这位博主写的很好,可以参考如下
通过el-upload结合xlsx获取表格数据_chaochao2_0的博客-CSDN博客
https://blog.csdn.net/chaochao2_0/article/details/112431673