csv - Javascript: set filename to be downloaded -


i'm using plugin generate csv file table, file being downloaded "download" filename, how can change filename e.g. dowload.csv

var csv = $("#table").table2csv({delivery:'download'}); window.location.href = 'data:text/csv;charset=utf-8,'+ encodeuricomponent(csv); 

i wrote tool can use save file downloads folder of local machine custom filename, if that's possible on client's machine.

as of writing, need chrome, firefox, or ie10 specific capability, tool falls-back un-named download if that's that's available, since better nothing...

for use:

download(csv, "dowload.csv", "text/csv"); 

and magic code:

function download(strdata, strfilename, strmimetype) {     var d = document,         = d.createelement("a");         strmimetype= strmimetype || "application/octet-stream";       if (navigator.mssaveblob) { // ie10         return navigator.mssaveblob(new blob([strdata], {type: strmimetype}), strfilename);     } /* end if(navigator.mssaveblob) */       if ('download' in a) { //html5 a[download]         a.href = "data:" + strmimetype + "," + encodeuricomponent(strdata);         a.setattribute("download", strfilename);         a.innerhtml = "downloading...";         d.body.appendchild(a);         settimeout(function() {             a.click();             d.body.removechild(a);         }, 66);         return true;     } /* end if('download' in a) */       //do iframe dataurl download (old ch+ff):     var f = d.createelement("iframe");     d.body.appendchild(f);     f.src = "data:" +  strmimetype   + "," + encodeuricomponent(strdata);      settimeout(function() {         d.body.removechild(f);     }, 333);     return true; } /* end download() */ 

update: added future-resistant ie routine

update2: checkout the evolved version on github includes dataurl , blob support.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -