Last active
November 10, 2017 23:06
-
-
Save farskid/2e663266a271402335c64712e4946eae to your computer and use it in GitHub Desktop.
Export to CSV from client side in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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