Is it possible to write data to file using only JavaScript?
Some suggestions for this -
- If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
- If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
- To store some information on the client side that is considerably small, you can go for cookies.
- Using the HTML5 API for Local Storage.
Read / Write txt file on server using only javascript without involving any server side language
Only if JavaScript is your server side language (e.g. with Node.JS).
Servers don't let you write files to them by default. That would be a horrible security problem.
Writing data to a local text file with javascript
Our HTML:
<div id="addnew">
<input type="text" id="id">
<input type="text" id="content">
<input type="button" value="Add" id="submit">
</div>
<div id="check">
<input type="text" id="input">
<input type="button" value="Search" id="search">
</div>
JS (writing to the txt file):
function writeToFile(d1, d2){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("data.txt", 8, false, 0);
fh.WriteLine(d1 + ',' + d2);
fh.Close();
}
var submit = document.getElementById("submit");
submit.onclick = function () {
var id = document.getElementById("id").value;
var content = document.getElementById("content").value;
writeToFile(id, content);
}
checking a particular row:
function readFile(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("data.txt", 1, false, 0);
var lines = "";
while (!fh.AtEndOfStream) {
lines += fh.ReadLine() + "\r";
}
fh.Close();
return lines;
}
var search = document.getElementById("search");
search.onclick = function () {
var input = document.getElementById("input").value;
if (input != "") {
var text = readFile();
var lines = text.split("\r");
lines.pop();
var result;
for (var i = 0; i < lines.length; i++) {
if (lines[i].match(new RegExp(input))) {
result = "Found: " + lines[i].split(",")[1];
}
}
if (result) { alert(result); }
else { alert(input + " not found!"); }
}
}
Put these inside a .hta
file and run it. Tested on W7, IE11. It's working. Also if you want me to explain what's going on, say so.
Writing to files in Node.js
There are a lot of details in the File System API. The most common way is:
const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
// Or
fs.writeFileSync('/tmp/test-sync', 'Hey there!');
Related Topics
How to Prevent Form from Being Submitted
How to Get the Selected Radio Button'S Value
Where to Place JavaScript in an HTML File
Dynamically Set Value of a File Input
Resize Html5 Canvas to Fit Window
Is There an Onselect Event or Equivalent For HTML ≪Select≫
Jsfiddle Code Not Working in My Own Page
JavaScript - Append HTML to Container Element Without Innerhtml
Jquery: Simulating a Click on a ≪Input Type="File" /≫ Doesn't Work in Firefox
Difference Between Properties and Attributes in Html
Clearing ≪Input Type='File' /≫ Using Jquery
Get Dom Content of Cross-Domain Iframe
Contenteditable, Set Caret At the End of the Text (Cross-Browser)
Detect All Changes to a ≪Input Type="Text"≫ (Immediately) Using Jquery
Html5 Canvas Resize (Downscale) Image High Quality
How to Fix Getimagedata() Error the Canvas Has Been Tainted by Cross-Origin Data