Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created January 5, 2014 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/8263718 to your computer and use it in GitHub Desktop.
Save joyrexus/8263718 to your computer and use it in GitHub Desktop.
Generate data attachments

Programmatically generate a CSV table along with a download link for it.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
a {
margin: 50px;
font-family: "Helvetica Neue";
font-weight: 700;
font-size: 75px;
color: steelblue;
text-decoration: none;
letter-spacing: -5px;
-webkit-font-smoothing: antialiased;
}
a:hover {
color: orange;
}
</style>
<body>
<script>
var rows = [ 'A,B,C', '0,1,2', '3,4,5' ];
var csv = rows.join("\n"); // join rows with newlines
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + encodeURI(csv);
a.download = 'data.csv';
a.innerText = 'click me!';
document.body.appendChild(a);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment