downloadFile()
For best results, use the function as part of a user-initiated DOM event.
function downloadFile(content, type = 'text/plain', filename) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([content], { type }));
a.download = filename;
a.click();
}
The default character encoding for the text/plain
MIME type is actually US-ASCII
, which might be a problem. You may want to make the UTF-8 encoding explicit with text/plain;charset=utf8
.