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.
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>
JavaScript running in a web page displayed in a browser cannot access the client file system.
But you can use API's
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With