Download CSV file through ajax post

Download CSV file through ajax post

Hi there,

I'm just trying to download a file generated from PHP with ajax. Even if I set headers PHP and contentType in Ajax parameters, I have still an issue as I can't have my response in a file but in my callback success as a parameter.

NB: I must send a lot of data with ajax, so I have to use POST method.

My PHP code :
<?php
header('Content-type: text/csv');
header('Content-disposition: attachment;filename="' . $csvFilename . '"');

echo $csvContent;

return;
?>

My JS:
$.ajax({
      url: url,
      type: 'POST',
      data: data,
      dataType: 'json',
      contentType: 'text/csv',
      context: this,
      success: function(response) {
            // Here is my response. Now, I want it in a single file.
      }
});

Thanks for all !