title: Need help getting AJAX call to download file
body: <p>So I am working on a website page where the user selects some elements to make a specific report. I then have them click on a button, which makes an ajax call. This call (correctly) calls a method in the controller that creates a .csv file based off of the parameters. </p>
  
  <p>However, the hard part is that, after I create this file, I want the user to download it in the usual manner-with the whole popup and putting it in the download folder and all that.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc">this</a> stack overflow question that is more or less doing what I am now. Except it was written pre-ASP.NET Core, and it's not really working with my code. Here is my code right now:</p>
  
  <pre><code>&lt;div class="mybutton"&gt;
      &lt;button type="button" class="btn btn-info" style="background-color:#587992" id="CreateExcel" onclick="CreateFile()"&gt; Create an Excel File for this Data &lt;/button&gt;
  &lt;/div&gt;
  
  ...
  
  &lt;script&gt;
  ...
  
  function CreateFile(){
     array = getParameters();
      $.ajax({
              type: "POST",
  
              data: { 'array': array },
              cache: false,
              traditional: true,
  
              url: '@Url.Action("CreateCsvFile")',
              success: function (data) {
                  if (data) {
                      var element = data;
                      window.location = 'Download?fileGuid=' + element.FileGuid 
                             + '&amp;filename=' + element.FileName;
                  }
              }
          });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>And then here is my controller code:</p>
  
  <pre><code>public ActionResult CreateCsvFile(string[] array)
          {
              List&lt;OperatorInterventions&gt; TempList = new List&lt;OperatorInterventions&gt;();
  
              //Logic to apply the List to the passed in parameters in this line...
  
              var memoryStream = new MemoryStream();
              using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
  
              using (var csv = new CsvWriter(writer))
              {
                  csv.WriteRecords(TempList);
  
              }
              string handle = Guid.NewGuid().ToString();
              memoryStream.Position = 0;
              TempData[handle] = memoryStream.ToArray();
  
              //return File(memoryStream, "text/csv", "clients.csv");
              return new JsonResult(new { FileGuid = handle, FileName = "newreport.csv" });
  
  
          }
  
  
          [HttpGet]
          public virtual ActionResult Download(string fileGuid, string fileName)
          {
              if (TempData[fileGuid] != null)
              {
                  byte[] data = TempData[fileGuid] as byte[];
                  return File(data, "text/csv", fileName);
              }
              else
              {
                  // Problem - Log the error, generate a blank file,
                  //           redirect to another controller action - whatever fits with your application
                  return new EmptyResult();
              }
          }
  </code></pre>
  
  <p>Right now, when I return from the Ajax call to CreateCsvFile, I attempt to handle data. When I step through the call, I can see that data/element contains something like <code>"{"fileGuid":"0123","fileName":"newreport.csv"}"</code>, but in the URL call that window.location uses, it element.FileGuid and element.FileName end up being "undefined". I previously had <code>JSON.parse(data)</code> in there, but that caused an error, and <code>JSON.stringify(data)</code> did nothing.</p>
  
  <p>Additionally, I suspect that even if I managed to get fileGuid and fileName to not be undefined in the url, I'm not totally sure if it would end up calling the Download file.</p>
  
  <p>Is there a way to fix the undefined? Is there a better way to do this Ajax call-to-download in general?</p>
  
  <p>Thanks</p>