Skip to content

Back to Snippets


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 })); = filename;;

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.