Get/set file encoding with javascript's FileReader

后端 未结 1 1447
南方客
南方客 2020-12-25 12:05

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.

I have

1条回答
  •  囚心锁ツ
    2020-12-25 12:37

    If your HTML page is in UTF-8 and your file is in 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:

     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.

    From the unicode mapping here and from Delan Azabani answer, you should manage to build a function that convert char by char your string in CP1251 to UTF-8.

    0 讨论(0)
提交回复
热议问题