1

I want to edit and write the data into the file which is on the disk and I want to save changes back to the same file using jquery. For this i have a button and textarea where I have edited the data and get the data before editing from json file as html5 :

function saveFile(data){
    
    $.getJSON("../custom_scripts/sub.json", function(data){
      
         var updatedData = $("#result").val();
         $("sub.json").replaceWith(updatedData);
        
    });
}
  <form class="fileForm">
                <input type="file" value="Choose file" onchange="onFileSelected(event)">
                <br><br>
                <textarea id="result" rows="20" cols="100"></textarea>
                <button type="button" id="savable" class="btn btn-default" title="Save Changes" onclick="saveFile();"> Save</button>
</form>

After selecting the file the data gets edited and the additional data which I have entered is the updatedData but it

1
  • You can't write on client's disk ! Commented Oct 13, 2015 at 7:34

2 Answers 2

1

you can't write a JSON to a file on the clientside due to security reasons. (Otherwise you have access to the filesystems of your website's users.) You would have to use a server-side language for this, and store the file on the server-side.

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

1 Comment

The file which I have tried to edit is json file it is edited but couldn't save it :(
0

For security, you can't write on client's disk.
The best way to save localy is to use localStorage.
You shoul read that page from MDN about webStorage.
some samples here.

var myContent = 'I am the content to save';
localStorage.setItem('mySaved', myContent);

and later to retrieve it :

 var myContent = localStorage.getItem('mySaved');

4 Comments

Yes.. I have used localstorage before to store the input field data as you mentioned :) Thank you. I want to add data externally not the hard coded :(
just go with it if you don't need to save server-side, it is a matter of design !
As it is a json file I want to add an item or many items each time i want to save so what can I do for that @Anonymous0day
use localStorage.setItem('mySavedJson', JSON.stringify( myJSON )); and later var mySavedJson = JSON.parse( localStorage.getItem('mySavedJson' ) );

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.