The approach to generating and and triggering download dialog is incorrect. A dialog (download) cannot be initiated from a background process(AJAX call)
I am trying to create/download a CSV file on button click. When i simply try to create the CSV the code works fine as below:
Drupal button :
$form['Export'] = array(
'#type' => 'submit',
'#value' => t('Export'),
"#ajax" => array(
"callback" => "export_csv",
),
Processing code:
$out = fopen('filename.csv', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);
the csv file is created and stored in the root directory.
However when i try to add headers the code below fails with ajax error and no debug info
$fichier = 'inscriptions.csv';
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$fichier\"" );
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen('php://output', 'w');
//processing to create file csv
fputcsv($out, $info, ";");
fclose($out);