我想知道是否可以使用 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 我有以下功能:
import React, { useState } from "react";
const Sheet = () => {
const [matrix, setMatrix] = useState([
[null, null, null],
[null, null, null],
[null, null, null]
]);
const handleChange = (row, column, event) => {
let copy = [...matrix];
copy[row][column] = +event.target.value;
setMatrix(copy);
console.log(matrix);
};
return (
<div className="sheet">
<table>
<tbody>
{matrix.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.map((column, columnIndex) => (
<td key={columnIndex}>
<input
type="number"
onChange={e => handleChange(rowIndex, columnIndex, e)}
/>
</td>
))}
</tr>
))}
</tbody>
</table>
</div> …Run Code Online (Sandbox Code Playgroud)