使用 javascript 的 FileReader 获取/设置文件编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12448595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get/set file encoding with javascript's FileReader
提问by thebravoman
I am struggling with the following problem. Using javascript I would like to change the character set of a file content and display this content to the user.
我正在努力解决以下问题。使用 javascript 我想更改文件内容的字符集并将此内容显示给用户。
I have an input:file form. On change I am reading the content
我有一个输入:文件形式。在更改时,我正在阅读内容
$('#form input:file').change(function(event){
file = this.files[0];
reader = new FileReader();
reader.onload = function(event) {
result = event.target.result.replace(/\n/g,'<br />');
$('#filecontents').html(result);
});
reader.readAsText(file);
})
The file is in Windows-1251. I would like to convert the content of the file to another encoding and after that present it to the user.
该文件位于 Windows-1251 中。我想将文件的内容转换为另一种编码,然后将其呈现给用户。
Is this possible to achieve with javascript?
这可以用javascript实现吗?
Regards
问候
回答by Florian Parain
If your HTML page is in UTF-8 and your file is in ISO-8859-1.
如果您的 HTML 页面是 UTF-8,而您的文件是 ISO-8859-1。
This is working:
这是有效的:
reader.readAsText(file, 'ISO-8859-1');
I don't have any Windows-1251 file so I was not able to test it but it looks like that the 'CP1251' is supported (by Google Chrome at least), so:
我没有任何 Windows-1251 文件,所以我无法对其进行测试,但似乎支持“CP1251”(至少由 Google Chrome 支持),因此:
reader.readAsText(file, 'CP1251');
If none of this is working. Then you should change the formatting manually. Unfortunately, I am not aware of any JavaScript library that does the trick.
如果这些都不起作用。然后您应该手动更改格式。不幸的是,我不知道有任何 JavaScript 库可以做到这一点。
From the unicode mapping hereand from Delan Azabanianswer, you should manage to build a function that convert char by char your string in CP1251 to UTF-8.
从这里的 unicode 映射和Delan Azabani 的答案中,您应该设法构建一个函数,将 CP1251 中的字符串逐个字符转换为 UTF-8。