title: Create file from AJAX response data and force download
body: <p>I need to make an AJAX call from a page, sending some JavaScript data to a PHP file, which creates a file based on that data. I then want this file to be downloaded by the user.</p>
  
  <p>So is it an option, to make a force download of a file created by PHP (specifically PHPExcel in my case) via an AJAX request?</p>
  
  <p>The code shown below does not yet send any JavaScript data, but does generate a file.</p>
  
  <p>This is my PHP script, which creates a file using PHPExcel:</p>
  
  <pre><code>public function renderTasksToExcel($tasksData){
  
  
      $objPHPExcel = new PHPExcel();
  
      // Set properties
      $objPHPExcel-&gt;getProperties()-&gt;setCreator("TRW");
      // ...
  
      // Add some data
  
      $objPHPExcel-&gt;setActiveSheetIndex(0);
      $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('A1', 'Hello');
      // ...
  
      // Rename sheet
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');
  
  
      // Save Excel 2007 file
      $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
      $file = '/export.' . date('Y-m-d-H-i-s').'.xlsx';
      $objWriter-&gt;save($file);
  
      // hotovo
     return $this-&gt;sendResponse(new FileResponse($file));
  }
  </code></pre>
  
  <p>And this is AJAX call:</p>
  
  <pre><code>$.ajax({
       type: "POST",
       url: {plink Tasks:tasksToExcel},
       data: ajaxData,
       success:function(data){
          $('#download').html(data);
       }
   })
  </code></pre>