Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
  • I have a text file in my local machine.
  • And i have a button in jsp page.
  • On click of the button, i need to get the text file contents.
  • And the file has n number of contents.

Can anyone give me javascript function to achieve this.

share|improve this question
Why javacript you mean probably java – CloudyMarble Feb 22 at 8:39
This Question Related to stackoverflow.com/questions/4950567/… – Snowp Feb 22 at 8:39
not the java code... i need it in javascript function... – Rachel Feb 22 at 8:43
You should look into javascript File API html5rocks.com/en/tutorials/file/dndfiles – guy mograbi Feb 22 at 8:44

You should specify in your question that you want client side file reading as I see a lot are referring to server side reading.

You should have a look in FileAPI - an HTML 5 Javascript addition that allows JavaScript to read file content via the file input.

I am working on code example for you - but here is a good site you should read

http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=4Fhi9T4mEAA

Without FileAPI - you can still use the file input field in form with target="some iframe" - then let the server upload the file and return the text. ( FormData allows uploading files in Ajax but it is not supported in all browsers ).

So File API is your way to go Here is how you do it with File API

<input type="file"/>
<script>
$(function(){
            $("input").change(function(e){
                    console.log(["file changed",e]);
                var myFile = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function(e){
                    console.log(["this is the contents of the file",e.target.result]);
                };
                reader.readAsText(myFile)

            });
        }
)
</script>

You can also implement a drag/drop interface (like google gmail has )

        $("div").on("dragover",function(e){
            e.dataTransfer = e.originalEvent.dataTransfer;
                e.stopPropagation();
                e.preventDefault();
                e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.

        }).on("drop",function(e){
                    e.dataTransfer = e.originalEvent.dataTransfer;
                    e.stopPropagation();
                    e.preventDefault();
                    console.log(["selected files", e.dataTransfer.files])});
share|improve this answer
its working. i got it. thanks. – Rachel Feb 22 at 9:18

use jquery load()

Load data from the server and place the returned HTML into the matched element.

Eg: $('#result').load('1.txt');

share|improve this answer
1  
The question is about a file on the local machine, not on the server. – DocMax Feb 22 at 8:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.