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.

Community
  • 1
  • 1
user1631651
  • 231
  • 3
  • 7
  • 9
  • This post should provide you with the best answer: http://stackoverflow.com/questions/585234/how-to-read-and-write-into-file-using-javascript – Conrad Lotz Sep 04 '12 at 08:38
  • @user1631651 see my below answer that's a working sample.. – Sark Sep 04 '12 at 11:02

3 Answers3

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>
Sark
  • 4,458
  • 5
  • 26
  • 25
1

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

But you can use API's

Peru
  • 2,871
  • 5
  • 37
  • 66
  • 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 Sep 04 '12 at 10:46
  • @user1631651 see my above answer that's a working sample.. – Sark Sep 04 '12 at 11:00
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);
}
sandeep patel
  • 436
  • 4
  • 16
  • i am looking for a code which can access a file..either in local filesystem or through url.. please help. – user1631651 Sep 04 '12 at 10:47
  • 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. – sandeep patel Sep 04 '12 at 10:59