如何使用HTML5和Javascript同时读取两个文本文件
How to read simultaneously two text files using HTML5 and Javascript
我正在使用HTML5和JavaScript来读取文本文件,但现在我需要读取2个文本文件,并执行与以前相同的操作,但针对这两个文件。
对于我一直在尝试的第二个文件:var file2 = files[1];
并在 html 输入中添加一个multiple
,但我如何为reader.onload = function (e) {
部分执行此操作?我想以相同的方式解析两个文件,而不仅仅是一个。
这是代码(简化):
<!DOCTYPE html>
<html>
<head>
<title>Read and Parse Lynis Log</title>
<script>
function processFiles(files) {
var file = files[0];
var reader = new FileReader();
var textParsed = [];
reader.onload = function (e) {
var output = document.getElementById("fileOutput");
output.textContent = e.target.result;
var text = e.target.result;
var lines = text.split("'n");
for (var i= 0; i < lines.length; i++) {
textParsed[i] = lines[i];
}
var testsPerformed = null;
var suggestions = [];
var suggestion = null;
var auxSug = null;
for (var j = 0; j < lines.length; j++) {
if (textParsed[j].includes("tests_executed")){
testsPerformed = textParsed[j];
}
if (textParsed[j].includes("suggestion[]")) {
suggestion = textParsed[j];
suggestions.push(suggestion);
}
}
if (typeof(Storage) !== "undefined" && textParsed.length >= 1) {
//Store
localStorage.setItem('storedText', textParsed);
localStorage.setItem('tests', testsPerformed);
localStorage.setItem('suggestions', suggestions);
}
};
reader.readAsText(file);
}
</script>
</head>
<body>
<input id="fileInput" placeholder=":input" type="file" size="50" onchange="processFiles(this.files)">
<div id="fileOutput"></div>
</body>
</html>
您只需要为每个文件初始化一个 FileReader,然后为每个文件启动 readAsText。函数将是相同的,我已经修改了输出,因此加载的每个文件都不会清除内容。
<!DOCTYPE html>
<html>
<head>
<title>Read and Parse Lynis Log</title>
<script>
function processFiles(files) {
var file = files[0];
var textParsed = [];
function onReadAsText(e) {
var output = document.getElementById("fileOutput");
output.textContent = output.textContent + e.target.result;
var text = e.target.result;
var lines = text.split("'n");
for (var i= 0; i < lines.length; i++) {
textParsed[i] = lines[i];
}
var testsPerformed = null;
var suggestions = [];
var suggestion = null;
var auxSug = null;
for (var j = 0; j < lines.length; j++) {
if (textParsed[j].includes("tests_executed")){
testsPerformed = textParsed[j];
}
if (textParsed[j].includes("suggestion[]")) {
suggestion = textParsed[j];
suggestions.push(suggestion);
}
}
if (typeof(Storage) !== "undefined" && textParsed.length >= 1) {
//Store
localStorage.setItem('storedText', textParsed);
localStorage.setItem('tests', testsPerformed);
localStorage.setItem('suggestions', suggestions);
}
};
for (var i = 0; i < files.length; i++){
var reader = new FileReader();
reader.onload = onReadAsText;
reader.readAsText(files[i]);
}
}
</script>
</head>
<body>
<input id="fileInput" placeholder=":input" type="file" size="50" onchange="processFiles(this.files)" multiple>
<div id="fileOutput"></div>
</body>
</html>
相关文章:
- 如何使用grunt durandal输出两个文件
- 使用 Node js 从另一个文件调用两个文件
- 在两个文件之间共享按钮变量
- 如何将包含在 eval 方法中的两个文件作为线程运行
- 如何将代码分成两个文件,并仍然使其在node.js中工作
- Ajax 调用时出错 ->发送两个文件而不是一个文件
- 创建一个包含两个文件 json 的 Angularjs 过滤器
- JavaScript:如何最有效地管理跨两个文件的数据导入
- 两个文件使用超级测试与摩卡导致 EADDRINUSE
- 我可以在同一本地网络但不在同一主机中的两个文件之间使用PostMessage吗
- 从Array结果中获取两个文件
- 两个文件输入表单中的文件值相同
- 简化php表单验证-将两个文件合并为一个文件
- 如何检查两个文件是否具有相同的内容
- 复制/b除两个文件外的同一类型的所有文件
- JavaScript/PowerShell:比较两个文件并将结果保存到文本文件
- Gulp-用Gulp ruby sass创建两个文件
- 如何在Node中的两个文件之间共享模块私有数据
- 在JavaScript中连续播放两个文件
- 如何链接多个javascript文件,其中两个文件都有onload事件