javascript:如何逐行解析FileReader对象 [英] javascript: how to parse a FileReader object line by line

查看:112
本文介绍了javascript:如何逐行解析FileReader对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a javascript/HTML5 page that I want to use to pull in a file and check each line to see whether it is greater than 240 chars:

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

I have things parsing correctly now, but they're not rendering correctly. Here's my updated code:

<!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>

我可以运行跟踪并看到输出变量正确填充,但我得到的所有输出是:超过240个字符的路径:没有 output.join( )部分渲染正确 - 有什么想法吗?

I can run a trace and see that the output variable is populating correctly, but all that I'm getting for output is: Paths with more than 240 characters: without the output.join() part rendering correctly -- any thoughts?

推荐答案

我想你应该把

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

onload 回调中。当您尝试使用它时,似乎尚未填充输出。所以:

inside onload callback. Seems that output is not yet populated when you try to use it. So:

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

这篇关于javascript:如何逐行解析FileReader对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆