【问题标题】:javascript: how to parse a FileReader object line by linejavascript:如何逐行解析 FileReader 对象
【发布时间】:2013-03-08 05:49:56
【问题描述】:

我有一个 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 html


    【解决方案1】:

    显而易见的方法(如果您可以容忍一次读取所有文件)是用换行符分割它。

    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) { /* ... */ });
    

    【讨论】:

    • 为什么要这样使用 forEach 构造函数?
    • 它不是构造函数,它是 Arrays 上的一个方法……不管怎样,这只是个人喜好问题。
    【解决方案2】:

    我猜你应该放

    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>';
    };
    

    【讨论】:

    • 做到了!我猜这是本地范围问题?
    • @fox 否。读取文件内容是异步操作,所以当您尝试使用 output 时,它还没有被填充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多