title: Download CSV button
body: <p>I have a list of users and I want to export it as a CSV file. I binded the button to an Angular controller.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;a class="btn btn-secondary-icon" href="" ng-click="csv()"&gt;Download CSV&lt;/a&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>$scope.csv = function() {
          $scope.export.cols = _.keys(_.pick($scope.data.columns, function(col) {
              return parseInt(col.checked) === 1;
          }));
  
          $http({
              url: $scope.dataUrl,
              data: $.param({
                  export: 1,
                  csv: $scope.export
              }),
              method: 'POST',
              headers: {
                  'Content-Type': 'application/x-www-form-urlencoded'
              }
          }).success(function(data) {
              $rootScope.$broadcast('ErrorOnResponse', data);
  
              if (data.csv) {
                  Download.download(
                      'yarooms-report-' + moment().format('YYYY-MM-DD') + '.csv',
                      'application/csv',
                      data.csv
                  );
  
                  Analytics.log(Analytics.event.EXPORT, Analytics.category.REPORTING);
              }
          });
      };
  </code></pre>
  
  <p>My problem occurs when it comes to writing the PHP function that grabs the data from my DB. I even tried to export a simple hardcoded array, but it won't work.  When I click the button, it makes a request to the server, but that's all, no downloaded file, nothing.</p>
  
  <p>My testing PHP function:</p>
  
  <pre><code>$file = 'php://memory'
  $list = array (
                  array('aaa', 'bbb', 'ccc', 'dddd'),
                  array('123', '456', '789'),
                  array('"aaa"', '"bbb"')
              );
  $f = fopen($file, 'rw+');
  foreach ($list as $fields) {
       fputcsv($fp, $fields);
              }
  rewind($f);
  $csv = stream_get_contents($f);
  
  fclose($f);
  
  Support_Model_Entity::logStat('Exported users CSV');
  
  $this-&gt;jsonResponse(array(
      'csv' =&gt; $csv
  ));
  </code></pre>