When I use readAsText(file,'utf-8'), I ran into a problem. Because I use the utf8 format to parse the file, when the user file is not in utf8 encoding format, the parsing result is likely to be wrong. How can I use Js to get the encoding format of the user file to remind the user to upload the utf8 file? Under Windows, I judge whether the first 3 bytes of the file are BOM, but Mac will not add BOM to utf8 files, so can anyone help? thank you very much
const encodeReader = new FileReader();
encodeReader.onload = function (e) {
const view = new Uint8Array(e.target.result as ArrayBuffer);
let zwsp = "";
for (const num of view) {
zwsp += num.toString(16);
}
if (zwsp.toUpperCase() === "EFBBBF") {
console.log("it is utf-8");
}
};
encodeReader.readAsArrayBuffer(file.slice(0, 3));