javascript:如何逐行解析FileReader对象

问题描述 投票:9回答:2

我有一个javascript / HTML5页面,我想用它来拉入一个文件并检查每一行,看它是否大于240个字符:

编辑:我现在解析正确的东西,但它们没有正确渲染。这是我更新的代码:

<!DOCTYPE HTML>

<html>

    <head>

        <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">

    </head>
    <body>

    <input type="file" id="input" name="file" multiple />
    <br>
    <output id="files"></output>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

        <script>

            if (window.File && window.FileReader && window.FileList && window.Blob) {
                // Great success! All the File APIs are supported.
            } else {
                alert('The File APIs are not fully supported in this browser.');
            }


            function handleFileSelect(evt) {
                var files = evt.target.files; // FileList object


                // files is a FileList of File objects. List some properties.
                var output = [];
                for (var i = 0, f; f = files[i]; i++) {

                    var reader = new FileReader();

                    reader.onload = function(e) {
                            // Print the contents of the file
                            var text = e.target.result;

                            var lines = text.split(/[\r\n]+/g); // tolerate both Windows and Unix linebreaks

                            for(var i = 0; i < lines.length; i++) {
                                if (lines[i].length > 240){
                                    output.push('<li>' + lines[i] + '<br>');
                                }
                            }
                    };

                    reader.readAsText(f,"UTF-8");

                }
                document.getElementById('files').innerHTML = 'Paths with more than 240 characters: <br><ul>' + output.join('') + '</ul>';
            }

            document.getElementById('input').addEventListener('change', handleFileSelect, false);


        </script>
    </body>
</html>

我可以运行跟踪并看到output变量正确填充,但我得到的所有输出是:Paths with more than 240 characters:没有正确渲染output.join()部分 - 任何想法?

javascript html5
2个回答
3
投票

我猜你应该放

document.getElementById('files').innerHTML = 'Paths with more than 240 characters: <br><ul>' + output.join('') + '</ul>';

onload内部回调。似乎output在您尝试使用时尚未填充。所以:

reader.onload = function (e) {
    // all your code ...

    // now can safely print output
    document.getElementById('files').innerHTML = 'Paths with more than 240 characters: <br><ul>' + output.join('') + '</ul>';
};

4
投票

显而易见的方法(如果你能够容忍一次读取文件)是按换行分割它。

var lines = text.split(/[\r\n]+/g); // tolerate both Windows and Unix linebreaks
for(var i = 0; i < lines.length; i++) { /* do something with lines[i] */ }
// or in modern JavaScript,
lines.forEach(function(line) { /* ... */ });
© www.soinside.com 2019 - 2024. All rights reserved.