0

I've made a function which create a csv file using a json. I've found some tips to have a button in order to download the file, but I'm having issues to have the file name like "*****.csv". I especially need to have the extension ".csv" but I couldn't find topics dealing about that.

Here is my 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 someone know what I have to write in my "window.open" line to get that ?

Thanks.

CocoFlouq
  • 27
  • 7

1 Answers1

0

use this header: Content-Disposition: attachment; filename="downloaded.csv"

yellowsir
  • 741
  • 1
  • 9
  • 27