How to save the output of a particular variable in a text file (javascript)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th Jul 2020, 6:28 AM
coal
coal - avatar
1 Answer
+ 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!!!
29th Jul 2020, 6:49 AM
Gyan Prakash Singh Jaruhar
Gyan Prakash Singh Jaruhar - avatar