【问题标题】:Change Javascript BLOB encoding to ANSI instead of UTF-8将 Javascript BLOB 编码更改为 ANSI 而不是 UTF-8
【发布时间】:2020-05-07 11:00:39
【问题描述】:

我想知道是否可以使用 Javascript 和使用 ANSI 编码的 BLOB 保存一个简单的 txt 文件。

此时我有一个脚本,它创建一个带有 CRLF 行结尾但使用 UTF-8 编码的 txt 文件。

可以用 ANSI 编码保存吗?我需要这个来在需要 ANSI 而不是 UTF-8 的“旧”Windows 程序上导入 txt 文件。

这是我使用的示例: https://jsfiddle.net/UselessCode/qm5AG/

let textFile = null;

function makeTextFile () {
    let text = `Some text with nice line endings\nand special characters like é and ü.`;

    const data = new Blob([text], {
        type: "text/plain",
        endings: "native"
    });

    if (textFile !== null) {
        window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    return textFile;
}

【问题讨论】:

标签: javascript encoding utf-8 blob ansi


【解决方案1】:

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

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

(async()=> {
const text = `Some text with nice line endings\nand 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。

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

(async () => {
  const text = `Some text with nice line endings\nand 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>

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2019-10-25
    相关资源
    最近更新 更多