javascript - How to have a specific file name when download -
this question has answer here:
i've made function create csv file using json. i've found tips have button in order download file, i'm having issues have file name "*****.csv". need have extension ".csv" couldn't find topics dealing that.
here code :
function json2csv() { var myjson = "http://******/get_json_test.php?callback=?"; $('.filter').each(function(i, obj){ var input = $(obj).find("input")[0]; myjson += '&'+input.name+'=' + input.value; }); $('.display').each(function(i,obj){ myjson += '&'+$(obj).attr('table')+'|'+$(obj).attr('titre')+''; }); $.getjson(myjson, function(data){ var line = ''; var str = ''; if(data.length != 0){ $.each(data[0], function(index, valeur){ //pour chaque colonne var line = ''; var value = index; line += value + ';'; str += line; //+ '\r\n'; }); str = str.slice(0, -1); str += '\n'; $.each(data, function(i,ti){ $.each(ti, function(index, valeur){ var line = ''; var valeur = valeur; line += valeur + ';'; str += line; // + '\r\n'; }); str = str.slice(0, -1); str += '\n'; }); //end each window.open("data:application/str;charset=utf-8," + encodeuricomponent(str)); } //end if }); //end getjson } //end json2csv
would know have write in "window.open" line ?
thanks.
use header: content-disposition: attachment; filename="downloaded.csv"