Skip to content

Instantly share code, notes, and snippets.

@basith374
Last active April 6, 2020 07:26
Show Gist options
  • Save basith374/0a850c8c41566a053d4fa5d05c94976b to your computer and use it in GitHub Desktop.
Save basith374/0a850c8c41566a053d4fa5d05c94976b to your computer and use it in GitHub Desktop.
javascript file download
function downloadCSV(content, filename) {
download(content, 'text/csv', filename + '.csv')
}
/**
* @param {string} content - The file content in raw string format
* @param {string} type - The filetype eg. text/plain, application/json, image/svg
* @param {string} filename - The filename with extension
*/
function download(content, type, filename) {
let link = document.createElement("a");
let data = new Blob([content], { type });
let url = URL.createObjectURL(data);
link.href = url;
link.style.cssText += ";visibility:hidden";
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment