title: Download csv file from Backend
body: <p>I am trying to create a csv file and insert my data and download it from the browser. I am creating the csv file in the express js Backend and send the link to react js frontend to download through browser.</p>
  <p>I am using fast csv plugin to write the data into csv. Here is my code.</p>
  <pre><code>async function dataToCSV(data) {
      var fileName = &quot;report.csv&quot;;
      var ws = await fileSystem.createWriteStream('../' + fileName);
      var csv;
      await fastcsv.write(data, { headers: true }).on(&quot;finish&quot;, function () {
          console.log(ws);
          csv = &quot;&lt;a href='../report.csv' download='report.csv' id='download-link'&gt;&lt;/a&gt;&quot;;
      }).pipe(ws);
      return csv;
  }
  </code></pre>
  <p>After getting the output from this method, I am sending this to the front end.</p>
  <pre><code>return dataToCSV(jsonData).then((response) =&gt; {
              return {
                  headers,
                  statusCode: 200,
                  body: response,
                  type: 'binary'
              };
          }).catch((e) =&gt; {
              return {
                  headers,
                  statusCode: 400,
                  body: {
                      error: e.message
                  }
              };
          })
  </code></pre>
  <p>Front end get the response as given below.</p>
  <pre><code>&lt;a href='report.csv' download='report.csv' id='download-link'&gt;&lt;/a&gt;
  </code></pre>
  <p>Then I use following code to download it through browser using react js.</p>
  <pre><code> var div = document.createElement('div');
   div.innerHTML = response.trim();
   document.body.appendChild(div);
   var downloadLink = document.getElementById('download-link');
   if (downloadLink !== null) {
       downloadLink.click();
   }
   document.body.removeChild(div);
  </code></pre>
  <p>File was created in the backend I can confirm. But when it is downloaded from the browser, it gets failed. There is no error in logs. Browser says</p>
  <blockquote>
  <p>Failed - No file</p>
  </blockquote>
  <p>I cannot understand why it fails. I think response is sent to the frontend before file is created or it can't identify the path where the file is created. But I am not sure about it. Please help me find a solution for this.</p>