Here is my response in a formwidget request controller:
...
$headers = array(
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-type' => 'text/csv; charset=UTF-8',
'Content-Disposition' => "attachment; filename=" . $fileName . '"',
'Expires' => '0',
'Pragma' => 'public',
);
return Response::download($filePath, $fileName, $headers);
...
I send the request via data-request
attribute, as described in here:
Handle File Downloads using the AJAX Framework - Updates and Announcements - October Talk (octobercms.com)
<button
id="birthday-subscription-export-csv"
type="button"
data-id="<?= $id; ?>"
onclick="$(this).data('request-data', { id: $(this).data('id') })"
data-request="onExportCsv"
data-request-download
data-browser-target="_blank"
data-load-indicator="Downloading CSV..." class="btn btn-primary"
>
Export CSV
</button>
Everything works fine, but the filenames keep being cryptic like T0JkSNIB.csv
or 3SSRIYGB.csv
. The $fileName
would be a nice and readable string.
I could not find an answer to this anywhere else.