setting is the following: I have a homepage where I display a diagram that has been constructed using comma seperated values from within the page. I\'d like to give users th
The most comprehensive solution I have run across is using FileSaver.js, handling many potential cross-browser issues for you with fallbacks.
It takes advantage of the Blob
object as other posters have done, and the creator even creates a Blob.js polyfill in case you need to support browser versions that don't support Blob
Tested on Safari 5, Firefox 6, Opera 12, Chrome 26.
<a id='a'>Download CSV</a>
<script>
var csv = 'a,b,c\n1,2,3\n';
var a = document.getElementById('a');
a.href='data:text/csv;base64,' + btoa(csv);
</script>
HTML5
<a id='a' download='Download.csv' type='text/csv'>Download CSV</a>
<script>
var csv = 'a,b,c\n1,2,3\n';
var data = new Blob([csv]);
var a = document.getElementById('a');
a.href = URL.createObjectURL(data);
</script>