Changer le nom d'un fichier au téléchargement
Bonjour à tous,
J'ai créé une fonction permettant de sortir un fichier CSV à partir d'un json.
Ce fichier peut se télécharger en cliquant sur un bouton.
J'aimerais pouvoir changer le nom de ce fichier lors du clic sur le bouton (pour l'instant il s'appelle "téléchargement" par défaut).
Mon plus gros souci est que l'extension n'est pas affichée (je voudrais en effet avoir au moins le .csv).
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
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 |
Sauriez vous ce que je dois changer dans la ligne :
Code:
window.open("data:application/str;charset=utf-8;" + encodeURIComponent(str));
afin d'avoir un nom comme "Requête.csv" ?
Merci de votre aide,
Corentin.