Discussions » Development

Wrong text encoding of read local files

§
Posted:
Edited:
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
§
Posted:

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

§
Posted:
Fetch doesnot work for me on local files
wOxxOmMod
§
Posted:

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');

Post reply

Sign in to post a reply.