0
How to save the output of a particular variable in a text file (javascript)?
How can we save the output of a particular variable in a text file? I want to call a variable inside a function and save it in a txt file I also want each file to have a specific name that I want like sum.txt sum1.txt variable names with a number I want it to be in javascript not PHP
1 Odpowiedź
+ 3
Sorry to say but if you mean writing the file to users disk, then you simply cannot do that because writing to a file directly using js would have had a great sequrity risk.
All you can do is to ask browser to download a file with variable content with custom name but that will always be on /sdcard/downloads/yourfile.txt on phone and C:\\downloads\yourfile.txt on windows(not sure about windows though, someone correct me if im wrong).
If youre talking doing it server side using node.js:
Then you can do like
const fs = require('fs');
fs.writeFile("/tmp/test", your_var, function(err) {
if(err) {
return console.log(err);
} else {
console.log("The file was saved!");
}
});
Hope this helps!!!