Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  145] [ 2]  / answers: 1 / hits: 12746  / 10 Years ago, fri, february 14, 2014, 12:00:00

I have an array of



myarray = [ram, ram, jan, jan, feb, feb]



Is it possible to create a .txt file which will have this value in a table format?


More From » jquery

 Answers
9

Frontend:



If your code is going to be on the frontend (browser), you can use the following:



var row_width = 40;

var content = ;
content += Username + new Array(row_width + 1).join( ) + Passwordn;
content += ******** + new Array(row_width + 1).join( ) + ********n;

for (var i = 0; i < myarray.length; i += 2) {
content += myarray[i] + new Array(row_width - myarray[i].length + 9).join( );
content += myarray[i+1];
content += n;
}

// Build a data URI:
uri = data:application/octet-stream, + encodeURIComponent(content);

// Click on the file to download
// You can also do this as a button that has the href pointing to the data URI
location.href = uri;


Here's a link to a working frontend example: Fiddle



Name the file as something.txt.



Backend:



If you want to use Node.js on the backend to generate and save this file, you need to use the following:



After looping through the array and creating the 'content' string as above, you save the file as follows:



var fs = require('fs');

fs.writeFile(/path_to_file/table.txt, content, function (err){
if (err) {
console.log(err);
} else {
console.log(File saved);
}
});


I haven't tried that code yet, but feel free to let me know if it works for you. I hope that's what you need.



NOTE that the frontend code might not be supported by some outdated/bad browsers


[#47726] Thursday, February 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
valentinam

Total Points: 166
Total Questions: 117
Total Answers: 81

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;