虽然我上面的答案是所问问题的最佳答案。我希望这个答案能帮助一些需要将文件作为二进制字符串读取的人:
const reader = new FileReader();
reader.readAsBinaryString(this.fileToImport);
就我而言,该文件位于 utf-16 中,我尝试将其读入 XLSX:
const wb = XLSX.read(bstr, { type: "binary" });
结合上面的两个链接,我首先删除了表明它是 UTF-16 (0xFFFE) 的前两个字符,然后使用此链接创建正确的数字(但我认为它实际上提供了 UTF-7 编码)
https://stackoverflow .com/a/14601808/1405208
最后,我应用了第二个链接来获得正确的 UTF-8 数字集:https ://stackoverflow.com/a/14601808/1405208
我最终得到的代码:
decodeUTF16LE(binaryStr) {
if (binaryStr.charCodeAt(0) != 255 || binaryStr.charCodeAt(1) != 254) {
return binaryStr;
}
const utf8 = [];
for (var i = 2; i < binaryStr.length; i += 2) {
let charcode = binaryStr.charCodeAt(i) | (binaryStr.charCodeAt(i + 1) << 8);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
} else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f));
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (charcode & 0x3ff));
utf8.push(
0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f)
);
}
}
return String.fromCharCode.apply(String, utf8);
},