Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

make file from array save js

async function saveCSV () {
  // (A) ARRAY OF DATA
  var array = [
    ["Job", "job@doe.com", "123456"],
    ["Joe", "joe@doe.com", "234567"],
    ["Joi", "joi@doe.com", "345678"],
    ["Jon", "jon@doe.com", "456789"],
    ["Jou", "jou@doe.com", "987654"],
    ["Joy", "joy@doe.com", "876543"],
  ];
 
  // (B) ARRAY TO CSV STRING
  var csv = "";
  for (let row of array) {
    for (let col of row) { csv += col + ","; }
    csv += "
";
  }
 
  // (C) CREATE BLOB OBJECT
  var myBlob = new Blob([csv], {type: "text/csv"});
 
  // (D) FILE HANDLER & FILE STREAM
  const fileHandle = await window.showSaveFilePicker({
    suggestedName : "demo.csv",
    types: [{
      description: "CSV file",
      accept: {"text/csv": [".csv"]}
    }]
  });
  const fileStream = await fileHandle.createWritable();
 
  // (E) WRITE FILE
  await fileStream.write(myBlob);
  await fileStream.close();
}
Source by code-boxx.com #
 
PREVIOUS NEXT
Tagged: #file #array #save #js
ADD COMMENT
Topic
Name
9+9 =