I am working with a contact form plugin, written by an external developer several years ago. I want to trigger a file download upon successful completion of the form.

As it stands, I have an onPost event listener in a separate plugin which fires if the form data is valid and stored in the database correctly:

craft()->on('capture_submissions.onPost', array($this, 'triggerDownload'));

I have tried several things such as redirecting to a controller action:

$url = UrlHelper::getActionUrl('myplugin/whitepaper/download', array('id' => $id));

Also tried to use sendFile:

craft()->request->sendFile($filepath, $file_contents);

This works, however I would like the page to refresh first, displaying a confirmation message and then to trigger the download. As it stands, the download is triggered but the page isn't reloaded so no success message and the form data is still displayed.


The way craft()->request->sendFile works, is that it responds to the HTTP(S) request by sending your file of choice to the browsers. As you can only return one answer to an HTTP request, you'd have to first redirect people to a confirmation page. On that page, you could have a download button, and use some Javascript to force the download to take place on page load or short thereafter. Don't leave the button out, as not everyone has JS enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.