如何使用HTML5和Javascript同时读取两个文本文件

时间:2016-04-21 11:46:59

标签: javascript html html5 filereader

我使用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>

1 个答案:

答案 0 :(得分:0)

您只需要为每个文件初始化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>