dudheit
dudheit

Hi,

I try to implement a simple export function in a backend list. If have put a button to the backend list form ...

<button
        class="btn btn-success oc-icon-print"
        disabled="disabled"
        onclick="$(this).data('request-data', {
        checked: $('.control-list').listWidget('getChecked')
        })"
        data-request="onExportGalleyProof"
        data-trigger-action="enable"
        data-trigger=".control-list input[type=checkbox]"
        data-trigger-condition="checked"
        data-request-success="$(this).prop('disabled', false)"
        data-stripe-load-indicator>
            Generate galley proof
    </button>

... and added the controller function:

public function onExportGalleyProof()
{
    if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds) > 0) {

        $html = ... // generates a special html site

        file_put_contents(temp_path('galleyproof.html'), $html);

        return Response::download(temp_path('galleyproof.html'));

    } else {
        Flash::error('You have to select an article');
    }
}

However, unfortunately, that doesn't work, as the download doesn't start. What is the problem? How can I start a download via AJAX request?

dudheit
dudheit

Ok, I found a solution with the help of daftspunk here

The idea is simply to redirect to a ordinary controller function and start the download there:

  <button
        class="btn btn-success oc-icon-print"
        disabled="disabled"
        onclick="$(this).data('request-data', {
        checked: $('.control-list').listWidget('getChecked')
        })"
        data-request="onGenerateGalleyProof"
        data-trigger-action="enable"
        data-trigger=".control-list input[type=checkbox]"
        data-trigger-condition="checked"
        data-request-success="$(this).prop('disabled', false)">
            Generate galley proof
    </button>

So we have to add another controller function:

public function onGenerateGalleyProof() {
   ...
    file_put_contents(temp_path('galleyproof.html'), $html);
   // Redirect to another function to start the actual download
   return Backend::redirect('dudheit/magazine/articles/download-galley-proof');
   ....
}

public function downloadGalleyProof() {
    // Here we can make use of the download response
    return Response::download(temp_path('galleyproof.html'));
}
manrox.drag17392
manrox.drag17392

great yeah that works

1-3 of 3