I have a function that successfully downloads a blob as text; however, its label (or ID, not exactly sure) appears as "blob:file:///*". Is there a way to rename this?
Here's my script:
function download_as_txt(options) {
let file = new Blob([options.data.toString()], { type: options.type }),
url = URL.createObjectURL(file),
a = document.createElement("a")
;
a.href = URL.createObjectURL(file);
a.download = options.name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
};
download_as_txt({
name: `${pages[active_page]["label"]} Export - ${current_epoch}.txt`,
data: config,
type: "text",
});
Here's what I've looked into:
• new Blob - option properties appear to allow 'type' and 'endings'
• new File - let file = new File([options.data.toString()], 'test.txt', { type: options.type }), // same as blob, and 'test.txt' doesn't appear anywhere
• troubleshooting - console.log(a) // looks like the name is tied to the 'href' attribute, so it looks like my current "temporary url" method may not work
In the meantime I'll look into using something like fs, but I'd still like to open an explorer window so my users can choose their download directory without needing to fill out a form. ...maybe have them select a directory, download to it, and alert when finished.
Edit:
I made a workaround for Electron:
• dialog.showOpenDialog
• fs.writeFile
• alertify.alert
The code doesn't look as clean as downloading a new blob, but at least I'm not manipulating the DOM (beyond alertify, anyway)