Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading and writing json file using javascript [duplicate]

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.

like image 722
user1631651 Avatar asked Sep 04 '12 08:09

user1631651


2 Answers

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>
like image 85
Sark Avatar answered Sep 22 '22 02:09

Sark


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

But you can use API's

like image 26
Peru Avatar answered Sep 21 '22 02:09

Peru