Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active November 18, 2022 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/8b2512a8f14c0ff701808a4d5d0de354 to your computer and use it in GitHub Desktop.
Save DarrenSem/8b2512a8f14c0ff701808a4d5d0de354 to your computer and use it in GitHub Desktop.
download.js: SAVE data TO file (aka 'export/download') plus openInNewWindow(data, type = "octet-stream")
/////// download.js: SAVE data TO file (aka 'export/download')...
//// const download_MIN=(d,f=`file-${+new Date}.txt`,t="octet-stream")=>{const h=URL.createObjectURL(new Blob([d].slice(void 0===d),{type:t})),s=null!==f;return Object.assign(document.createElement("a"),{href:h,[s&&"download"]:f,target:"_blank"}).click(),s&&URL.revokeObjectURL(h)};
const download = (
data
, filename = `file-${+new Date}.txt` // null means download: filename OMITTED = DISPLAY data in browser
, type = "octet-stream" // default is ~ "application/octet-binary"
) => {
const href = URL.createObjectURL(new Blob( // https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob
[data].slice(data === undefined)
, { type }
));
const save = filename !== null;
Object.assign(document.createElement("a"), {
href
, [save && "download"]: filename // , download: filename OMITTED = DISPLAY data in browser instead of saving file e.g. [blob:<location.origin>/79b9264f-c0f4-4354-a655-d6cc084acb69]
, target: "_blank" // NEW TAB for [blob:<location.origin>/abc-12-34-def]
}).click();
return save && URL.revokeObjectURL(href);
};
const openInNewWindow = (d, t) => download(d, null, t);
const openInNewWindow_standalone=(d,t="octet-stream")=>Object.assign(document.createElement("a"),{href:URL.createObjectURL(new Blob([d].slice(d===void 0),{type:"text"===t?t+"/plain;charset=utf-8":t})),target:"_blank"}).click();
// const openInNewWindow_standalone = (data, type = "octet-stream") => Object.assign(
// document.createElement("a"), {
// href: URL.createObjectURL(
// new Blob([data].slice(data === undefined), {
// type: type !== "text" ? type : (type + "/plain;charset=utf-8")
// })
// ),
// target: "_blank",
// }
// ).click(); // utf-8 suffix by default if type === "text" (to easily retain emojis like this...)
//var b = "🤦‍♂🌞 have a great weekend "; console.log(b); console.log(b = new Blob([b], {type: "text/plain; charset=utf-8"}), b = URL.createObjectURL(b)); window.open(b); !!b;
// "🤦‍♂️🌞 have a great weekend "
// true
/////// examples with simple plaintext dataString (output = various values for MIME type)
const dataString = "\nthe\n\n\tcontents"
// download text as myFilename.ext
download(dataString, "myFilename.ext");
// display text inside new tab (method 1/2)
download(dataString, null);
// display text inside new tab (method 2/2)
openInNewWindow(dataString);
// display text AS PNG (will not be usable, of course) inside new tab (via method 2)
openInNewWindow(dataString, "image/png");
// display text AS ZIP (will prompt to download, because .ZIP!) inside new tab (via method 2)
openInNewWindow(dataString, "application/zip");
// display text AS Windows' custom MIME type for ZIP (will prompt to download, because .ZIP!) inside new tab (via method 2)
openInNewWindow(dataString, "application/x-zip-compressed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment