Ron*_*ink 8 javascript encoding blob ansi utf-8
我想知道是否可以使用 Javascript 和 ANSI 编码的 BLOB 保存一个简单的 txt 文件。
\n\n目前,我有一个脚本创建一个带有 CRLF 行结尾但采用 UTF-8 编码的 txt 文件。
\n\n可以用ANSI编码保存吗?我需要这个来导入需要 ANSI 而不是 UTF-8 的“旧”Windows 程序上的 txt 文件。
\n\n这是我使用的示例:\n https://jsfiddle.net/UselessCode/qm5AG/
\n\nlet textFile = null;\n\nfunction makeTextFile () {\n let text = `Some text with nice line endings\\nand special characters like \xc3\xa9 and \xc3\xbc.`;\n\n const data = new Blob([text], {\n type: "text/plain",\n endings: "native"\n });\n\n if (textFile !== null) {\n window.URL.revokeObjectURL(textFile);\n }\n\n textFile = window.URL.createObjectURL(data);\n\n return textFile;\n}\nRun Code Online (Sandbox Code Playgroud)\n
曾经有一个选项使用TextEncoder API将 USVString 编码为任意编码,但这已从规范和浏览器中删除。
\n\n您需要使用库才能执行转换。在这里,我将使用inexorabletash/text-encoding:
\n\n(async()=> {\r\nconst text = `Some text with nice line endings\\nand special characters like \xc3\xa9 and \xc3\xbc.`;\r\nconst encoding = \'windows-1252\'; // a.k.a ANSI\r\n\r\nconst encoder = new TextEncoder(encoding, {\r\n NONSTANDARD_allowLegacyEncoding: true\r\n});\r\nconst data = encoder.encode(text); // `data` is an Uint8Array\r\nconst encoded_as_ANSI = new Blob([data]);\r\n\r\n// for demo only\r\nconst encoded_as_UTF8 = new Blob([text]);\r\n\r\nconst ANSI_read = await readAsText(encoded_as_ANSI, encoding);\r\nconst UTF8_read = await readAsText(encoded_as_UTF8, encoding);\r\n\r\nconsole.log("(ANSI)", ANSI_read);\r\nconsole.log("(UTF8)", UTF8_read);\r\n})();\r\n\r\nfunction readAsText(blob, encoding) {\r\n return new Promise(res => {\r\n const reader = new FileReader();\r\n reader.onload = e => res(reader.result);\r\n reader.readAsText(blob, encoding);\r\n });\r\n}Run Code Online (Sandbox Code Playgroud)\r\n<script>window.TextEncoder = null;// force installation of the polyfill</script>\r\n<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>\r\n<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding.js"></script>Run Code Online (Sandbox Code Playgroud)\r\n然而,按照这条路线,我们失去了结尾选项,因为这仅适用于字符串 blobParts。
\n\n因此,一种方法是首先创建一个带有结尾选项的 utf-8 Blob,然后将此 UTF-8 Blob 转换为 ANSI:
\n\n(async () => {\r\n const text = `Some text with nice line endings\\nand special characters like \xc3\xa9 and \xc3\xbc.`;\r\n const encoding = \'windows-1252\'; // a.k.a ANSI\r\n\r\n const utf8_blob = new Blob( [text], { endings: "native" } );\r\n const utf_8_txt = await utf8_blob.text();\r\n\r\n const encoder = new TextEncoder(encoding, {\r\n NONSTANDARD_allowLegacyEncoding: true\r\n });\r\n const data = encoder.encode(utf_8_txt); // now `data` is an Uint8Array\r\n const encoded_as_ANSI = new Blob([data]);\r\n\r\n const read_as_ANSI = await readAsText(encoded_as_ANSI, encoding)\r\n console.log(read_as_ANSI);\r\n})();\r\n\r\nfunction readAsText(blob, encoding) {\r\n return new Promise(res => {\r\n const reader = new FileReader();\r\n reader.onload = e => res(reader.result);\r\n reader.readAsText(blob, encoding);\r\n });\r\n}Run Code Online (Sandbox Code Playgroud)\r\n<script>window.TextEncoder = null;// force installation of the polyfill</script>\r\n<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding-indexes.js"></script>\r\n<script src="https://cdn.jsdelivr.net/gh/inexorabletash/text-encoding/lib/encoding.js"></script>Run Code Online (Sandbox Code Playgroud)\r\n