October CMS resources and help articles

Simple and to the point. Optimized by the community.

Return a file download from an AJAX handler

3
by OFFLINE, last modified on August 12th, 2019

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');
}

Discussion

2 comments

0
rizhenkov
Post on March 27th, 2020 8:15 AM

But what if downloadable data based on request POST params?

0
octobertricks@luketowers.ca
Post on April 22nd, 2020 5:55 PM

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.

We use cookies to measure the performance of this website. Do you want to accept these cookies?