Export javascript data to CSV file without server interaction -
if on nodejs server, write header, set mime type, , send it:
res.header("content-disposition", "attachment;filename="+name+".csv"); res.type("text/csv"); res.send(200, csvstring);
and because of headers, browser create download named csv file.
when useful data generated in browser, 1 solution getting in csv file use ajax, upload server, (perhaps optionally save there) , server send these headers become csv download @ browser.
however, 100% browser solution not involve ping-pong server.
so occurred me 1 open new window , try set header meta tag equivalent.
but doesn't work me in recent chrome.
i new window, , contains csvstring, not act download.
i guess expected either download in bottom tab or blank new window download in bottom tab.
i'm wondering if meta tags correct or if other tags needed.
is there way make work without punting server?
jsfiddle creating csv in browser (not working - outputs window no download)
var = [['n','sqrt(n)']]; // initialize array of rows header row 1st item for(var j=1;j<10;++j){ a.push([j, math.sqrt(j)]) } var csvrows = []; for(var i=0,l=a.length; i<l; ++i){ csvrows.push(a[i].join(',')); // unquoted csv row } var csvstring = csvrows.join("\n"); console.log(csvstring); var csvwin = window.open("","",""); csvwin.document.write('<meta name="content-type" content="text/csv">'); csvwin.document.write('<meta name="content-disposition" content="attachment; filename=data.csv"> '); csvwin.document.write(csvstring);
there's html5 download
attribute :
this attribute, if present, indicates author intends hyperlink used downloading resource when user clicks on link prompted save local file.
if attribute has value, value used pre-filled file name in save prompt opens when user clicks on link.
var = [['n','sqrt(n)']]; for(var j=1; j<10; ++j){ a.push([j, math.sqrt(j)]); } var csvrows = []; for(var i=0, l=a.length; i<l; ++i){ csvrows.push(a[i].join(',')); } var csvstring = csvrows.join("%0a"); var = document.createelement('a'); a.href = 'data:attachment/csv,' + encodeuricomponent(csvstring); a.target = '_blank'; a.download = 'myfile.csv'; document.body.appendchild(a); a.click();
tested in chrome , firefox, works fine in newest versions (as of july 2013).
works in opera well, not set filename (as of july 2013).
not seem work in ie9 (big suprise) (as of july 2013).
an overview on browsers support download attribute can found here
non-supporting browsers, 1 has set appropriate headers on serverside.
apparently there a hack ie10 , ie11, doesn't support download
attribute (edge however).
var = [['n','sqrt(n)']]; for(var j=1; j<10; ++j){ a.push([j, math.sqrt(j)]); } var csvrows = []; for(var i=0, l=a.length; i<l; ++i){ csvrows.push(a[i].join(',')); } var csvstring = csvrows.join("%0a"); if (window.navigator.mssaveoropenblob) { var blob = new blob([csvstring]); window.navigator.mssaveoropenblob(blob, 'myfile.csv'); } else { var = document.createelement('a'); a.href = 'data:attachment/csv,' + encodeuricomponent(csvstring); a.target = '_blank'; a.download = 'myfile.csv'; document.body.appendchild(a); a.click(); }
Comments
Post a Comment