0

How to read text from a txt file with one button to browse the file and other button to display text. Pls help me in getting the code. i have tried many codes but othing worked. some code was like this. Thanks in advance

<!DOCTYPE html>
<html>
  <head>
    <title>reading file</title>
    <script type="text/javascript">

        var reader = new FileReader();

        function readText(that){

            if(that.files && that.files[0]){
                var reader = new FileReader();
                reader.onload = function (e) {  
                    var output=e.target.result;
                    //process text to show only lines with "@":             
            output=output.split("\n").filter(/./.test, /\@/).join("\n");

                document.getElementById('main').innerHTML= output;
                };//end onload()
                reader.readAsText(that.files[0]);
            }//end if html5 filelist support
        } 
</script>
</head>
<body>
    <input type="file" onchange='readText(this)' />
    <div id="main"></div>
  </body>
</html>
2
  • you cannot read from files from JavaScript.Need to use Ajax. Commented Oct 29, 2013 at 11:58
  • @Tyagi You can using the FileReader api. Commented Oct 29, 2013 at 11:59

2 Answers 2

1

You should properly read an article like this: http://www.html5rocks.com/en/tutorials/file/dndfiles/

Dont think this line is working properly:

output=output.split("\n").filter(/./.test, /\@/).join("\n");

Try changing it to:

output=output.split("\n").filter(function(l) {
    //return /^\@/.test(l); // Starting with @
    return l.indexOf('@') > -1; // Containing @
}).join("\n");

It would be interesting to see if this would work as well:

output=output.split("\n").filter(/\@/.test.bind(/\@/)).join("\n");

The second arguments passed to the .filter method is the context:

array.filter(callback[, thisObject])

Sign up to request clarification or add additional context in comments.

Comments

0

Get the input file, use FileReader() to read it,on file load get text that matches the pattern. Finally display it through "main" div. Should work..

HTML :

<input id="fileInput" type="file"/>
<div id="main"></div>

jQuery :

$(function(){
        $("#fileInput").change(function(e){
            var myFile = e.target.files[0];
            var reader = new FileReader();
            reader.onload = function(e){
                var output = e.target.result;
                output=output.split("\n").filter(/./.test, /\@/).join("\n");
                $("#main").text(output);
            };
            reader.readAsText(myFile)
        });
    }
)

Demo

2 Comments

Actually i am new to javascript. so could u pls give me full code at a place so that I can run and understand better.
I've also posted the demo on the bottom part of my answer named Demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.