title: .NET Core Web API Return CSV in Reactjs
body: <p>I am returning a csv file from an API using a csvformatter code shown below
  API Call</p>
  <pre><code>[HttpGet(&quot;generatecsv/project/{projectId}/BookID/{bookId}&quot;)]
  [Produces(&quot;text/csv&quot;)]
      public IActionResult Getcsv(string projectId, string bookId)
      {
          try
          {
              Models model = GetModel();
              return Ok(model);
          }
          catch (Exception ex)
          {
              return BadRequest(ex.Message);
          }
      }
  </code></pre>
  <p>CSV Formatter</p>
  <pre><code>public class CSVOutputFormatter : OutputFormatter
      {
          public string ContentType { get; }
          public PDRDModels ViewModel { get; set; }
  
          public CSVOutputFormatter()
          {
              ContentType = &quot;text/csv&quot;;
              SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(ContentType));
          }
  
          public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
          {
              var response = context.HttpContext.Response;
              var filePath = string.Format(&quot;./DataExport/PDRD-{0}.csv&quot;, DateTime.Now.Ticks);
              ViewModel = context.Object as Models;
  
              using (var writer = new StreamWriter(filePath))
              using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
              {
                  csv.WriteField(&quot;StackUp Information&quot;);
                  csv.NextRecord();
                  csv.NextRecord();
                  WriteToCSV1(csv, ViewModel.Data1);
                  foreach (var pdrdModel in ViewModel.PDRDModelList)
                  {
                      csv.NextRecord();
                      csv.WriteField($&quot;Physical Rules&quot;);
                      csv.NextRecord();
                      WriteToCSV2(ViewModel.Data2);
                  }
              }
              response.Headers.Add(&quot;Content-Disposition&quot;, &quot;attachment;filename=Pdrd.csv&quot;);
              response.ContentType = &quot;text/csv&quot;;
  
              await response.SendFileAsync(filePath);
          }
      }
  }
  </code></pre>
  <p>The API returns the csv file as attachment when hit through the browser or Postman. But when hit through the UI code its just returning the csv response as a string not as an attachment.
  Below is the UI Code for hitting the above end Point.</p>
  <pre><code>export const getExportedValues = async (projectId: string, bookId: string) =&gt; {
      if (projectId &amp;&amp; bookId) {
          const finalUrl: string = `${getEnvConfig().projectsAPIUrl}/document/generatecsv/project/${projectId}/bookID/${bookId}`;
          let resp: any = await axios({ method: AxiosMethods.GET, url: finalUrl }).catch(err =&gt; errorFunction(err))
          if (!resp?.data?.error) {
              return resp?.data;
          }
      } else return {}
  
  }
  </code></pre>
  <p>what am I missing in my reactjs Code to let this download as attachment instad of plain csv text</p>