FileReader readAsText Wrong Encoding
Quick summary
You called readAsText on a non-UTF-8 file without specifying the encoding — the result has garbled characters.
Why this happens
File is encoded in GBK, Shift-JIS, or Latin-1 but read as UTF-8
No encoding argument passed to readAsText
BOM stripped or misinterpreted by the reader
Minimal example
const reader = new FileReader();
reader.onload = e => console.log(e.target.result); // garbled for GBK file
reader.readAsText(gbkFile); // defaults to UTF-8const reader = new FileReader();
reader.onload = e => console.log(e.target.result);
reader.readAsText(gbkFile, 'gbk'); // correct encodingreadAsText defaults to UTF-8, so a GBK file produces mojibake. Passing 'gbk' as the second argument decodes the bytes correctly.
How to diagnose
Inspect the result string for replacement characters (U+FFFD) or garbled text
Check the file encoding with a hex editor or file command
Try reading with an explicit encoding argument
How to fix
Pass the correct encoding: reader.readAsText(file, 'gbk')
Use TextDecoder with a detected encoding for more control
Convert the file to UTF-8 before reading if possible
How to prevent
Always store and transmit text files as UTF-8
Detect encoding (e.g., with jschardet) before reading