$(document).ready(function () {
$('#fileInput1').change(function () {
const file = this.files[0];
const reader = new FileReader();
reader.onload = function (event) {
const fileContent = event.target.result;
// 将'\r'和'\n'替换为适当的字符
// fileContent = fileContent.replace(/\r/g, '').replace(/\n/g, '<br>');
$('#fileContent1').html(fileContent);
};
reader.readAsText(file);
});
// $('#fileInput2').change(function () {
// const file = this.files[0];
// const reader = new FileReader();
// reader.onload = function (event) {
// const fileContent = event.target.result;
// // 将'\r'和'\n'替换为适当的字符
// fileContent = fileContent.replace(/\r/g, '').replace(/\n/g, '<br>');
// $('#fileContent2').text(fileContent);
// };
// reader.readAsText(file);
// });
$('#saveButton').click(function () {
const file1Text = $('#fileContent1').text().trim();
// const file2Text = $('#fileContent2').text().trim();
// 获取选择的文件名
const file1Name = $('#fileInput1').prop('files')[0].name;
// const file2Name = $('#fileInput2').prop('files')[0].name;
const file1Blocks = file1Text.split('\n\n');
// const file2Blocks = file2Text.split('\n\n');
// 保存同步后的文本
let syncedText = '';
// 遍历文件1块数组
file1Blocks.forEach(function (file1Block, index) {
// 解析序号
const blockIndex = parseInt(file1Block.split('\n')[0]);
// 检查序号是否在文件2的范围内
// if (blockIndex > 0 && blockIndex <= file2Blocks.length) {
// 获取对应序号的文件2块
// const file2Block = file2Blocks[blockIndex - 1];
// 提取文件1块的第1、2行
// const file1Lines = file1Block.split('\n').slice(0, 2);
// 提取文件1块的第1、2行
const file1Lines = file1Block.split('\n').slice(2);
// 提取文件2块除去第1、2行之外的内容
// const file2Lines = file2Block.split('\n').slice(2);
// 将文件1和文件2的内容行拼接起来
syncedText += `${file1Lines.join('\n')}\n`;
// }
console.log(syncedText);
});
// 创建Blob对象
const blob = new Blob([syncedText], { type: 'text/plain' });
// 创建下载链接
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
// 设置下载链接的名称为文件名
downloadLink.download = `synced_${file1Name}.txt`;
// 模拟点击下载链接
downloadLink.click();
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="file" id="fileInput1">
<!-- <input type="file" id="fileInput2"> -->
<pre id="fileContent1"></pre>
<!-- <pre id="fileContent2"></pre> -->
<button id="saveButton">保存为TXT</button>
</body>
</html>