Change Javascript BLOB encoding to ANSI instead of UTF-8 - Stack Overflow

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

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

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

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.

This is the example I used: /

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;
}

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

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

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.

This is the example I used: https://jsfiddle/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;
}
Share asked Jan 21, 2020 at 11:28 Ronald Groot JebbinkRonald Groot Jebbink 1452 gold badges2 silver badges9 bronze badges 1
  • stackoverflow./questions/32510273/… might answer your question - change the charset/encoding hinted to the browser – Thomas Timbul Commented Jan 21, 2020 at 12:38
Add a ment  | 

1 Answer 1

Reset to default 2

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.

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/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>
<script src="https://cdn.jsdelivr/gh/inexorabletash/text-encoding/lib/encoding.js"></script>

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

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/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>
<script src="https://cdn.jsdelivr/gh/inexorabletash/text-encoding/lib/encoding.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742295126a4416961.html

相关推荐

  • Change Javascript BLOB encoding to ANSI instead of UTF-8 - Stack Overflow

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

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信