Discussions » Development

Wrong text encoding of read local files

§
Posted: 2020-10-04
Edited: 2020-10-04

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: 2020-10-04

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

§
Posted: 2020-10-05

Fetch doesnot work for me on local files

wOxxOmMod
§
Posted: 2020-10-05

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.