7

Possible Duplicate:
How to read and write into file using JavaScript

can anybody provide sample code to read and write into file using javascript?

at present i am trying to read input from json file and display it in textboxes providing the user flexibility to edit the data. Edited data has to be written into json file.

2
  • This post should provide you with the best answer: stackoverflow.com/questions/585234/… Commented Sep 4, 2012 at 8:38
  • @user1631651 see my below answer that's a working sample.. Commented Sep 4, 2012 at 11:02

3 Answers 3

2

here is the sample html file, i have tested it with firefox working fine.

<!DOCTYPE html>
<html>
    <head>
        <script>        
            function handleFileSelect()
            {               
                if (window.File && window.FileReader && window.FileList && window.Blob) {

                } else {
                    alert('The File APIs are not fully supported in this browser.');
                    return;
                }   

                input = document.getElementById('fileinput');
                if (!input) {
                  alert("Um, couldn't find the fileinput element.");
               }
               else if (!input.files) {
                  alert("This browser doesn't seem to support the `files` property of file inputs.");
               }
               else if (!input.files[0]) {
                  alert("Please select a file before clicking 'Load'");               
               }
               else {
                  file = input.files[0];
                  fr = new FileReader();
                  fr.onload = receivedText;
                  fr.readAsText(file);
               }
            }

            function receivedText() {           
               //result = fr.result;
               document.getElementById('editor').appendChild(document.createTextNode(fr.result))
            }           

        </script>
    </head>
    <body>
        <input type="file" id="fileinput"/>
        <input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'>
        <div id="editor"></div>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

This does not show how to write JSON
1

JavaScript running in a web page displayed in a browser cannot access the client file system.

But you can use API's

2 Comments

is it possible if i deploy the html page in IIS and access the file using url..??if yes can you please provide sample code for the same...
@user1631651 see my above answer that's a working sample..
0

(No File programming in javascript) If you mean parsing json in javascript then :-

  1. you can use Douglas crockford JSON lib for parsing:- JSON.parse method Refer Link :- http://www.json.org/js.html

Example,

var abcd= "[{"name" : "sandeep"},{"name" :"Ramesh"}]"

abcd =JSON.parse(abcd);

for (var index=0;index<abcd.length;index++){

alert(abcd[i].name);
}

2 Comments

i am looking for a code which can access a file..either in local filesystem or through url.. please help.
Ok then, if your file is in a server then make ajax call through jquery $.post() and in callback do the parsing and then manipulate it . After that send it to server with another ajax call and save it in the desired location in server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.