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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -