简体   繁体   中英

Change the encoding of FileReader's result to UTF8

I have a small function that retrieves the contents of an uploaded text file.

upload() {
    let reader = new FileReader();
    reader.onload = () => {
        console.log(reader.result);
    }
    reader.readAsText(this.file);
}

It works fine when the encoding of the file uploaded is UTF8, but in my case (using an UTF16_LE encoded file) it adds random spacing between characters, changes some to something different, or transforms them to chinese.

My question is, how can I convert reader.result to UTF8 without having to manually re-save the file itself to UTF8?

FileReader.readAsText()函数具有可选的encoding参数,您可以使用该参数指定用于返回数据的编码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM