Answers.Body: <p>The way <code>craft()-&gt;request-&gt;sendFile</code> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-01T09:45:27.100Z
Answers.LastActivityDate: 2018-10-01T09:45:27.100Z
Answers.UserDisplayName: Paul
Body: <p>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.</p>
  
  <p>As it stands, I have an <code>onPost</code> event listener in a separate plugin which fires if the form data is valid and stored in the database correctly:</p>
  
  <p><code>craft()-&gt;on('capture_submissions.onPost', array($this, 'triggerDownload'));</code></p>
  
  <p>I have tried several things such as redirecting to a controller action:</p>
  
  <pre><code>$url = UrlHelper::getActionUrl('myplugin/whitepaper/download', array('id' =&gt; $id));
  craft()-&gt;request-&gt;redirect($url);
  </code></pre>
  
  <p>Also tried to use sendFile:</p>
  
  <p><code>craft()-&gt;request-&gt;sendFile($filepath, $file_contents);</code></p>
  
  <p>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.</p>
  
Tags: <plugin-development><events><craft2>
Title: Trigger download after form submission
ThreadText: # Trigger download after form submission
  
  (Asked by: username_0 on 2018-09-27)
  
  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));
  craft()->request->redirect($url);
  
  ```
  
  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.
  
  **Tags:** plugin-development, events, craft2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-01. Score: 1
  
  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.
  
  
  ---
  
  