title: Yii2 Ajax Request File Download
body: <p>In my Yii2 application i'm dynamically generating .zip files that i want to download through an Ajax call in one of my views.</p>
  
  <p>This is my code</p>
  
  <p><strong>_view.php</strong></p>
  
  <pre><code>$js = &lt;&lt;&lt;JS
      $('#button-download').on('click', function(){
  
          var tkn = $('meta[name="csrf-token"]').attr("content");
          var fields =  JSON.stringify($('#gridview').yiiGridView('getSelectedRows'));
  
          $.ajax({
              url: '$urlDownload', // Generated with Url::toRoute([someaction]);
              type: 'get',
              dataType: 'json',
              data: {
                  field: fields,
                  _csrf : tkn, 
              },
              success: function(data){
                  console.log(data);
                  // ?
                  // ?
                  // ?
              }
          });
      });
  JS;
  
  $this-&gt;registerJs($js);
  </code></pre>
  
  <p>This is my view file, here i register the $.ajax method that reach an action in my controller.</p>
  
  <p><strong>Controller.php</strong></p>
  
  <pre><code>/**
   * Download Action
   * 
   * @param integer $field
   * @return mixed
   */
  public function actionDownload($field) {
  
      if (Yii::$app-&gt;request-&gt;isAjax &amp;&amp; !empty(json_decode($field))) {
  
          // Bring some Data From Db
  
          // Create a new Zip
          $archive = new \ZipArchive();
          $now = $timestamp; // let's assume that the $now variable is a valid timestamp
  
          $zip_name = Yii::t('app', 'NameThisArchive').$now.'.zip';
  
          if (!$archive-&gt;open(Yii::getAlias('@app/runtime/foo').'/'.$zip_name, \ZipArchive::CREATE)) {
              throw new \Exception('Can\'t create the file');
          }
  
          foreach ($datafromdb as $record) {
  
              if ($somecondiction) {
  
                  // Generating files and adding them to my archive
  
                  $archive-&gt;addFile($path, '/'.Yii::t('app', 'somefileinpdf{0}.pdf', [$record-&gt;id]));
              }
          }
          $archive-&gt;close();
  
          // return ?
          // Yii::$app-&gt;response-&gt;sendFile($path, $zip_name);
  
      }
  }
  </code></pre>
  
  <p>In this controller i receive the data from the ajax request. I'm able to create the zip archive and save it to my runtime folder. </p>
  
  <p>Now, the problem, is how can i send this file to the ajax call. (in the .success() callback).</p>
  
  <p>What should i return ? </p>
  
  <p>Looking in the yii official forum i've found some response helper:</p>
  
  <pre><code>Yii::$app-&gt;response-&gt;sendFile("path", "Filename");
  </code></pre>
  
  <p>If I return that function <code>return Yii::$app-&gt;response-&gt;sendFile("path", "Filename");</code> </p>
  
  <p>No file is download in my browser.
  But looking in my Safari browser console i see that a xhr request (the one i send with ajax) return some data.</p>
  
  <p>So a response is given.
  How can i capture it with my Ajax function? </p>
  
  <p>Thanks for any help. </p>