Skip to content

Instantly share code, notes, and snippets.

@toddsiegel
Last active June 24, 2020 21:35
Show Gist options
  • Save toddsiegel/d41207378313c487d590503edc91fd6f to your computer and use it in GitHub Desktop.
Save toddsiegel/d41207378313c487d590503edc91fd6f to your computer and use it in GitHub Desktop.
Client-side CSV Download
const exportRows = [
["Name", "Email"],
["Foo Bar", "foo@bar.com"]
];
const csvContent = "data:text/csv;charset=utf-8," + exportRows.map((e) => e.join(",")).join("\n");
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "export.csv"); // https://caniuse.com/#feat=download
document.body.appendChild(link); // FF aparently needs this.
link.click();
document.body.removeChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment