将Javascript BLOB编码更改为ANSI而不是UTF-8 [英] Change Javascript BLOB encoding to ANSI instead of UTF-8

查看:247
本文介绍了将Javascript BLOB编码更改为ANSI而不是UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if it is possible to save a simple txt file with Javascript and BLOB with the ANSI encoding.

此刻,我有一个脚本来创建带有CRLF行尾但使用UTF-8编码的txt文件.

At this moment I have a script that creates a txt file with CRLF line endings, but with a UTF-8 encoding.

是否可以使用ANSI编码保存它?我需要此文件将txt文件导入需要ANSI而不是UTF-8的旧" Windows程序中.

Is it possible to save it with the ANSI encoding? I need this to import the txt file on a 'old' windows program that needs ANSI instead of UTF-8.

这是我使用的示例: 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;
}

推荐答案

过去使用

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/文本编码:

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\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.

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

所以一种方法是先创建一个 ends 选项的utf-8 Blob,然后将该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\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>

这篇关于将Javascript BLOB编码更改为ANSI而不是UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆