Answers.Body: <p>It's not possible in a web browser application / HTTP to do a download and then redirect.</p>
  <p>Since you're passing the list of cids in the redirect url, what you can do is in buildform add a link on the page to a download that uses the list of cids if present. Then add a &quot;page&quot; (<code>civix generate:page</code>) in your extension that responds to that link and does the same thing your createMembershipCard() function does.</p>
  <p>e.g. <code>civix generate:page CRM_Myextension_MembershipCard civicrm/myextension/membershipcard</code></p>
  <p>and then in buildForm you'd do something like</p>
  <pre><code>$cids = CRM_Utils_Request::retrieve('cids', 'String', $this);
  $url = CRM_Utils_System::url('civicrm/myextension/membershipcard', &quot;reset=1&amp;cids={$cids}&quot;);
  $form-&gt;assign('url', $url);
  CRM_Core_Region::instance('page-body')-&gt;add(array(
      'template' =&gt; 'CRM/Myextension/MembershipCard.tpl',
    ));
  </code></pre>
  <p>and in MembershipCard.tpl you'd have a link like <code>&lt;a href=&quot;{$url}&quot;&gt;{ts}Download membership card{/ts}&lt;/a&gt;</code></p>
  <p>and in CRM_Myextension_MembershipCard::run you do the same thing as createMembershipCard().</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-15T14:44:09.753Z
Answers.LastActivityDate: 2021-06-15T14:44:09.753Z
Answers.UserDisplayName: Demerit
Body: <p>Sorry for the newbie question...</p>
  <p>I modified <code>CRM_Contact_Form_Task_Label</code> class, to create my own Task. It creates necessary data for printing membership cards. If a contact has all necessary data, then these are printed to a file, if not, the contact should be shown as result of a Custom Search.</p>
  <p>I can't figure out why, but this code doesn't shows up the file, only redirects the page. File is shown up when I add <code>exit;</code> after the <code>$fileReady = self::createMembershipCard($rows);</code> row.</p>
  <p>Please advise me, how can I show up the file and then redirect the page.</p>
  <p>Thank you!</p>
  <pre><code>    function postProcess($params = NULL) {
  
          [...]
  
          $fileReady = FALSE;
          $fileReady = self::createMembershipCard($rows);
          if($fileReady) {
              $csvListOfIds = implode(&quot;,&quot;, $failed);
              CRM_Utils_System::redirect( CRM_Utils_System::url('civicrm/contact/search/custom', &quot;reset=1&amp;force=1&amp;csid=19&amp;cid={$csvListOfIds}&quot;));
          }
       }
  
    /**
     * Create export file for Membership Cards.
     *
     * @param array $contactRows
     *   Associated array of contact data.
     */
      public function createMembershipCard(&amp;$contactRows) {
          header('Content-Type: text/csv; charset=utf-8');
          header('Content-Disposition: attachment; filename=data.txt');
  
          $output = fopen('php://output', 'w');
          
          if(sizeof($contactRows) != 0) {
              foreach($contactRows as $item) {
                  $toOutput = $item['display_name'].';'.date('Y.m.d.').';2099.21.31.'.PHP_EOL;
                  fwrite($output, $toOutput);
              }
          }
          fclose($output);
          
          return TRUE;
      }
  </code></pre>
  
Tags: <files><redirect>
Title: Write a file, after them redirect
ThreadText: # Write a file, after them redirect
  
  (Asked by: username_0 on 2021-06-15)
  
  Sorry for the newbie question...
  
  I modified `CRM_Contact_Form_Task_Label` class, to create my own Task. It creates necessary data for printing membership cards. If a contact has all necessary data, then these are printed to a file, if not, the contact should be shown as result of a Custom Search.
  
  I can't figure out why, but this code doesn't shows up the file, only redirects the page. File is shown up when I add `exit;` after the `$fileReady = self::createMembershipCard($rows);` row.
  
  Please advise me, how can I show up the file and then redirect the page.
  
  Thank you!
  
  ```
      function postProcess($params = NULL) {
  
          [...]
  
          $fileReady = FALSE;
          $fileReady = self::createMembershipCard($rows);
          if($fileReady) {
              $csvListOfIds = implode(",", $failed);
              CRM_Utils_System::redirect( CRM_Utils_System::url('civicrm/contact/search/custom', "reset=1&force=1&csid=19&cid={$csvListOfIds}"));
          }
       }
  
    /**
     * Create export file for Membership Cards.
     *
     * @param array $contactRows
     *   Associated array of contact data.
     */
      public function createMembershipCard(&$contactRows) {
          header('Content-Type: text/csv; charset=utf-8');
          header('Content-Disposition: attachment; filename=data.txt');
  
          $output = fopen('php://output', 'w');
          
          if(sizeof($contactRows) != 0) {
              foreach($contactRows as $item) {
                  $toOutput = $item['display_name'].';'.date('Y.m.d.').';2099.21.31.'.PHP_EOL;
                  fwrite($output, $toOutput);
              }
          }
          fclose($output);
          
          return TRUE;
      }
  
  ```
  
  **Tags:** files, redirect
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-15. Score: 1
  
  It's not possible in a web browser application / HTTP to do a download and then redirect.
  
  Since you're passing the list of cids in the redirect url, what you can do is in buildform add a link on the page to a download that uses the list of cids if present. Then add a "page" (`civix generate:page`) in your extension that responds to that link and does the same thing your createMembershipCard() function does.
  
  e.g. `civix generate:page CRM_Myextension_MembershipCard civicrm/myextension/membershipcard`
  
  and then in buildForm you'd do something like
  
  ```
  $cids = CRM_Utils_Request::retrieve('cids', 'String', $this);
  $url = CRM_Utils_System::url('civicrm/myextension/membershipcard', "reset=1&cids={$cids}");
  $form->assign('url', $url);
  CRM_Core_Region::instance('page-body')->add(array(
      'template' => 'CRM/Myextension/MembershipCard.tpl',
    ));
  
  ```
  
  and in MembershipCard.tpl you'd have a link like `<a href="{$url}">{ts}Download membership card{/ts}</a>`
  
  and in CRM\_Myextension\_MembershipCard::run you do the same thing as createMembershipCard().
  
  
  ---
  
  