Skip to content

Instantly share code, notes, and snippets.

@farskid
Last active November 10, 2017 23:06
Show Gist options
  • Save farskid/2e663266a271402335c64712e4946eae to your computer and use it in GitHub Desktop.
Save farskid/2e663266a271402335c64712e4946eae to your computer and use it in GitHub Desktop.
Export to CSV from client side in Javascript
/*
* @param {string} content
* @param {string} fileName
*/
function downloadWithContent(content, fileName) {
// Create a virtual <a> element
var $a = document.createElement('a');
// Attach proper href
$a.attr('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent('\uFEFF') + encodeURIComponent(content));
// Attach download prop
$a.attr('download', fileName + '.csv');
// Append to body
document.body.append($a);
// Click to initiate download process
$a[0].click();
// Remove unncessary element
$a.remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment