且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Javascript BLOB 编码更改为 ANSI 而不是 UTF-8

更新时间:2023-11-27 12:31:40

曾经有一个选项使用 TextEncoder API 用于从 USVStrings 编码为任意编码,但这已从规范和浏览器中删除.

There used to be an option using the TextEncoder API to encode from USVStrings to arbitrary encodings, but this has been removed from specs and browsers.

您需要使用库才能执行转换.在这里,我将使用 inexorabletash/text-encoding:

You'd need to use a library in order to perform the conversion. Here, I'll use inexorabletash/text-encoding:

(async()=> {
const text = `Some text with nice line endings
and special characters like é and ü.`;
const encoding = 'windows-1252'; // a.k.a ANSI

const encoder = new TextEncoder(encoding, {
  NONSTANDARD_allowLegacyEncoding: true
});
const data = encoder.encode(text); // `data` is an Uint8Array
const encoded_as_ANSI = new Blob([data]);

// for demo only
const encoded_as_UTF8 = new Blob([text]);

const ANSI_read = await readAsText(encoded_as_ANSI, encoding);
const UTF8_read = await readAsText(encoded_as_UTF8, encoding);

console.log("(ANSI)", ANSI_read);
console.log("(UTF8)", UTF8_read);
})();

function readAsText(blob, encoding) {
  return new Promise(res => {
    const reader = new FileReader();
    reader.onload = e => res(reader.result);
    reader.readAsText(blob, encoding);
  });
}

<script>window.TextEncoder = null;// force installation of the polyfill</script>
<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>
<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding.js"></script>

无论如何,我们都放弃了 endings 选项,因为这仅适用于字符串 blobParts.

However going this route, we loose the endings option, since this applies only to string blobParts.

所以一种方法是首先创建一个 utf-8 Blob,使用 endings 选项,然后将此 UTF-8 Blob 转换为 ANSI:

So one way would be to first create an utf-8 Blob, with the endings option, then convert this UTF-8 blob to ANSI:

(async () => {
  const text = `Some text with nice line endings
and special characters like é and ü.`;
  const encoding = 'windows-1252'; // a.k.a ANSI

  const utf8_blob = new Blob( [text], { endings: "native" } );
  const utf_8_txt = await utf8_blob.text();

  const encoder = new TextEncoder(encoding, {
    NONSTANDARD_allowLegacyEncoding: true
  });
  const data = encoder.encode(utf_8_txt); // now `data` is an Uint8Array
  const encoded_as_ANSI = new Blob([data]);

  const read_as_ANSI = await readAsText(encoded_as_ANSI, encoding)
  console.log(read_as_ANSI);
})();

function readAsText(blob, encoding) {
  return new Promise(res => {
    const reader = new FileReader();
    reader.onload = e => res(reader.result);
    reader.readAsText(blob, encoding);
  });
}

<script>window.TextEncoder = null;// force installation of the polyfill</script>
<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>
<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding.js"></script>