From 41aa88f3872b1e9027770a9506a00d00b06934f2 Mon Sep 17 00:00:00 2001 From: David Joerg Date: Fri, 7 Mar 2014 15:12:57 -0500 Subject: [PATCH] use HTML5 download attribute rather than hitting a server --- export-csv.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/export-csv.js b/export-csv.js index 5e32cc5..d6a847d 100644 --- a/export-csv.js +++ b/export-csv.js @@ -17,7 +17,7 @@ // Options dateFormat = options.dateFormat || '%Y-%m-%d %H:%M:%S', itemDelimiter = options.itemDelimiter || ',', // use ';' for direct import to Excel - lineDelimiter = options.lineDelimeter || '\n'; + lineDelimiter = options.lineDelimeter || "%0A"; each (this.series, function (series) { if (series.options.includeInCSVExport !== false) { @@ -55,18 +55,19 @@ return csv; }; - // Now we want to add "Download CSV" to the exporting menu. We post the CSV - // to a simple PHP script that returns it with a content-type header as a - // downloadable file. - // The source code for the PHP script can be viewed at - // https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php + // Now we want to add "Download CSV" to the exporting menu. if (Highcharts.getOptions().exporting) { Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", onclick: function () { - Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', { - csv: this.getCSV() - }); + // http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction + var a = document.createElement('a'); + a.href = 'data:attachment/csv,' + this.getCSV(); + a.target = '_blank'; + a.download = this.title.text + '.csv'; + document.body.appendChild(a); + a.click(); + a.remove(); } }); }