JS

HOW TO SAVE FILES IN JAVASCRIPT

CREATE BLOB OBJECT var myBlob = new Blob(["Hello World"], {type: "text/plain"});

CREATE DOWNLOAD LINK var url = window.URL .createObjectURL(myBlob);

BLOB FILE DOWNLOAD (A)

01

ATTACH LINK TO BODY var anc = document.createElement("a");  anc.href = url; anc.download = "demo.txt"; document.body.appendChild(anc);

BLOB FILE DOWNLOAD (B)

02

SAVE TO FILE DIALOG BOX const fileHandle = await window.showSaveFilePicker();

CREATE BLOB OBJECT var myBlob = new Blob(["Hello World"], {type: "text/plain"});

WRITABLE FILE STREAM (A)

03

FILE STREAM const fileStream = await fileHandle.createWritable();

WRITE TO FILE STREAM await fileStream.write(myBlob); await fileStream.close();

WRITABLE FILE STREAM (B)

04

LOAD FILE SYSTEM MODULE var fs = require("fs");

WRITE FILE fs.writeFile(   "DEMO.TXT", "Foo bar!", "utf8",    (err, data) => {     console.log("Write complete");        console.log(err);     console.log(data); });

NODEJS WRITE FILE

05