Reputation: 11
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
Upvotes: 1
Views: 2790
Reputation: 3042
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');
Upvotes: 0
Reputation: 6992
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.
Upvotes: 1