Return a file download from an AJAX handler
7
Currently it is not possible to return a download response from an AJAX handler. You have to use a dedicated route or page that returns the response instead.
title = "Download"
url = "/trigger-download"
is_hidden = 0
==
function onStart()
{
$pathToFile = storage_path('your-file.pdf');
$fileName = 'download.pdf';
$headers = [
'Content-Type' => 'application/pdf'
];
return Response::download($pathToFile, $fileName, $headers);
}
==
You can redirect to this page from any AJAX handler and the browser will start a file download.
function onDownloadHandler()
{
return Redirect::to('trigger-download');
}
But what if downloadable data based on request POST params?
Then pass that information to your file generating route, either through GET params, routing params, or storing it in the Session and revalidating that the user has authorization to receive the generated data in your file generation handler.