首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Javascript BLOB编码改为ANSI,而不是UTF-8

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

Stack Overflow用户
提问于 2020-01-21 11:28:31
回答 1查看 6.3K关注 0票数 6

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

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

可以用ANSI编码保存它吗?我需要这个导入txt文件在一个‘旧的’窗口程序,需要ANSI,而不是UTF-8。

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

代码语言:javascript
运行
复制
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;
}
EN

Stack Overflow用户

发布于 2020-02-16 07:03:25

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

您需要使用一个库来执行转换。在这里,我将使用不可移除/文本编码

代码语言:javascript
运行
复制
(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);
  });
}
代码语言:javascript
运行
复制
<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。

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

代码语言:javascript
运行
复制
(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);
  });
}
代码语言:javascript
运行
复制
<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>

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59840163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档