title: Laravel Excel File download from Ajax Request
body: <p>I wanna generate the excel from laravel with PHPspreadsheet (PHP Lib) and then return the xlsx file to the frontend to trigger the download function. is that possible?</p>
  <p>JSX Part</p>
  <pre><code>axios
  .get(
      &quot;/excel/export/dashboardTable&quot;, {}
  )
  .then(resp =&gt; {
      //success callback
      if (resp.status == 200) {
          const blob = new Blob([resp.data], { type: &quot;application/vnd.ms-excel&quot; });
          let link = URL.createObjectURL(blob);
          let a = document.createElement(&quot;a&quot;);
          a.download = &quot;Customers.xlsx&quot;;
          a.href = link;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
      }
  })
  .catch(error =&gt; {
      _.forEach(error.response.data.errors, function (
          value,
          el
      ) {
          toastr.error(&quot;Error&quot;, value, {
              onHidden: function onHidden() { }
          });
      });
  })
  .finally(() =&gt; { });
  </code></pre>
  <p>Backend Part</p>
  <pre><code>$response = response()-&gt;streamDownload(function () use ($spreadsheet) {
      $writer = new xlsx($spreadsheet);
      $writer-&gt;save('php://output');
  }, &quot;Dashboard.xlsx&quot;);
  
  $response-&gt;setStatusCode(200);
  $response-&gt;headers-&gt;set('Content-Type', 'application/vnd.ms-excel');
  
  return $response;
  </code></pre>