Tartışmalar » Geliştirme

Wrong text encoding of read local files

§
Gönderildi: 04.10.2020
Düzenlendi: 04.10.2020

Hi,

As FileReader.readAsText(..., 'UTF-8'), as XHR return local content in forced singlebyte encoding, even for files saved in UTF-8, resulting in malformed words like "Meldinš", "bâtard" and similar.
I'd like to ask for a working method how to read local files in UTF8 the proper way. by setting concerned functions properly, or converting the charset ex-post, whatever works. Thanks for any ideas

woxxomMod
§
Gönderildi: 04.10.2020

Simply use fetch for example: fetch(url).then(r => r.text()).then(text => { console.log(text) })

§
Gönderildi: 05.10.2020

Fetch doesnot work for me on local files

woxxomMod
§
Gönderildi: 05.10.2020

Yep, I forgot about that.

Well, you can override the encoding in XHR:

var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.overrideMimeType('text/plain; charset=UTF-8');

Cevap paylaş

Yanıt göndermek için oturum açın.