title: Download file by ajax success
body: <p>I have a function that record a row in database everytime when someone download a file. I have modal, first user import user information - name, phone, country i ec... after this the button for download is active and he can download it ... how to download file with ajax?
  if render success and in console show content of file, but do not download it ...
  here is button:</p>
  
  <pre><code>&lt;div class="col-md-10 col-xs-12 broshura" &gt;
      &lt;a class="popupBroshura-trigger"&gt;
         &lt;div class="col-md-3 col-xs-3 pr0 pull-left  pt15"&gt;&lt;img src="/images/download.png" /&gt;&lt;/div&gt;
         &lt;div class="col-md-9 col-xs-9 pl0 pull-left"&gt;
            &lt;div id="six" class="button customBtn pl0"&gt;&lt;?=Yii::$app-&gt;OutData-&gt;getLabel(281);?&gt;&lt;/div&gt;
         &lt;/div&gt;
     &lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>here is a ajax function:</p>
  
  <pre><code> function downloadGallery() {
          var product_id = $('input[name="product_id"]').val();
          var names = $('input[name="StatisticBroshura[names]"]').val();
          var email = $('input[name="StatisticBroshura[email]"]').val();
          var firma = $('input[name="StatisticBroshura[firma]"]').val();
          var phone = $('input[name="StatisticBroshura[phone]"]').val();
          var flag = $('input[name="flag"]').val();
          var country = $('input[name="StatisticBroshura[country]"]').val();
          var captcha_code = $('input[name="StatisticBroshura[captcha_code]"]').val();
          var container = $('#hidden-result');
          $.ajax({
              method: 'post',
              url: '/admin/site/downloadbroshura',
              data :{
                  product_id: product_id,
                  names: names,
                  firma: firma,
                  country: country,
                  phone: phone,
                  flag: flag,
                  email: email,
                  captcha_code: captcha_code
              },
              success: function ( data ) {
                  var data = JSON.parse( data );
                  if(data['result'] == true){
                      if(data [ 'flag' ] == 1) {
                          $('input[name="StatisticBroshura[email]"]').val('');
                          $('input[name="StatisticBroshura[names]"]').val('');
                          $('input[name="StatisticBroshura[firma]"]').val('');
                          $('input[name="StatisticBroshura[country]"]').val('');
                          $('input[name="StatisticBroshura[phone]"]').val('');
                          $('input[name="StatisticBroshura[captcha_code]"]').val('');
                          $('.message-broshura').html(data['textError']);
                          $('.message-broshura').css('border', '2px solid');
                          $('html, body').animate({
                              scrollTop: $(".popupRequest").offset().top
                          }, 2000);
                          window.location.href = data [ 'file' ];
                      }else{
                          $('.message-broshura').html(data['textError']);
                          $('.message-broshura').css('border', '2px solid');
                          $('.message-broshura').css('color', 'cornflowerblue!important');
                          $('html, body').animate({
                              scrollTop: $(".popupBroshura").offset().top
                          }, 2000);
  
                      }
                  }
  
                  return false;
              }
          });
      }
  </code></pre>
  
  <p>and the public function in controller:</p>
  
  <pre><code>public function actionDownloadbroshura()
  {
   if (isset($_POST['product_id']) and !empty($_POST['product_id'])) {
   $result['result'] = true;
          $result['textError'] = '';
          $result['flag'] = '';
          $result['file'] = '';
          $flag = $_POST['flag'];
          $class = '';
          $first_step = Yii::$app-&gt;getRequest()-&gt;getQueryParam('first_step');
          $second_step = Yii::$app-&gt;getRequest()-&gt;getQueryParam('second_step');
          $product_step = Yii::$app-&gt;getRequest()-&gt;getQueryParam('product');
          $last = explode('-', $product_step);
          $id = end($last);
          $lastparent = explode('-', $second_step);
          $idparent = end($lastparent);
          $product = Product::findOne($id);
          $modelBroshura = new StatisticBroshura();
          $messageBroshura = "";
          $sessionBroshura = new Session;
          $sessionBroshura-&gt;open();
          $model = new Request();
          $message = '';
  
          if (session_status() == PHP_SESSION_NONE) {
              session_start();
          }
          if ($_SESSION['captcha']['code'] != $_POST['captcha_code']) {
              $result['textError'] = Label::findOne(65)-&gt;label;
              $result['flag'] = 2;
          } else {
              $result['textError'] = Label::findOne(310)-&gt;label;
              $result['flag'] = 1;
              $companyInfo = CompanyInfo::findOne(1);
              $headers = "MIME-Version: 1.0\r\n";
              $headers .= "Content-type: text/html; charset=utf-8\r\n";
              $headers .= "From: " . $companyInfo-&gt;email . "\r\n";
              $messageAdmin = "";
              $product = Product::findOne($_POST['product_id']);
              $modelBroshura = new StatisticBroshura();
              $file = $product-&gt;getBroshuraDownload($product-&gt;id);
              if (isset($file) &amp;&amp; $modelBroshura-&gt;save()) {
                  if (isset($_POST['StatisticBroshura'])) {
                      $request_broshura = new StatisticBroshura();
                      $request_broshura-&gt;names = $_POST['names'];
                      $request_broshura-&gt;firma = $_POST['firma'];
                      $request_broshura-&gt;email = $_POST['email'];
                      $request_broshura-&gt;phone = $_POST['phone'];
                      $request_broshura-&gt;country = $_POST['country'];
                      $request_broshura-&gt;product_id = $_POST['product_id'];
                      $request_broshura-&gt;save(false);
                  }
              }
  
              $messageBroshura = Label::findOne(306)-&gt;label;
              $messageAdmin .= "&lt;div style=\"width: 800px;\"&gt;&lt;center&gt;&lt;a href=\"http://" . $_SERVER['SERVER_NAME'] . "\"&gt;&lt;img src='http://" . $_SERVER['SERVER_NAME'] . "/images/logo-header.png' alt=\"OPTIXCO\" /&gt;&lt;/a&gt;&lt;/center&gt;&lt;h1&gt;&lt;center&gt;Изтеглена е брошура от : " . $_POST['email'] . "!&lt;/center&gt;&lt;/h1&gt;";
              $messageAdmin .= "&lt;table width=\"800px\" style=\"border-collapse: collapse; border: 1px solid black; margin-bottom: 30px;\"&gt;";
              $messageAdmin .= "&lt;tr style=\"background-color: #ddd; border: 1px solid black;\"&gt;&lt;td colspan=\"2\"&gt;&lt;h2&gt;&lt;center&gt;Информация на потребителя: &lt;/center&gt;&lt;/h2&gt;&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Имена: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $_POST['names'] . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Имейл: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $_POST['email'] . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Телефон: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $_POST['phone'] . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Държава: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $_POST['country'] . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Фирма/Организация: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $_POST['firma'] . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;tr style=\"border: 1px solid black;\"&gt;&lt;td style=\"border: 1px solid black;\"&gt;Брошура за продукт: &lt;/td&gt;&lt;td style=\"border: 1px solid black;\"&gt;" . $product-&gt;title . "&lt;/td&gt;&lt;/tr&gt;";
              $messageAdmin .= "&lt;/table&gt;&lt;/div&gt;";
  
              @mail($companyInfo-&gt;email2, "Изтегляне на брошура - " . $_SERVER['SERVER_NAME'], $messageAdmin, $headers);
  
              $result['file'] = Yii::$app-&gt;response-&gt;sendFile(Yii::getAlias('@frontend/web') . $file);
          }
          echo json_encode($result);
  }
  }
  </code></pre>
  
  <p>i tried with $result variable and with return the file, but not working with both methods ...</p>