title: Problem saving downloaded file on ASP.Net Core via download button on UI
body: <p>Any help with this would be much appreciated. I've got a datagrid and a download button on my UI. When the user selects a row in the grid and clicks download, it should pass the int id as parameter to my Download method on my controller, which processes the download. </p>
  
  <p>I have used Ajax/JQuery for this, which I am quite new to. From debugging it appears that it is sending two GET requests, one with my correct ID and one without, which is causing an empty file to be saved in the download. If i type the full URL with ID on the browser it downloads correctly, and my download method also works in my Unit testing, so I'm doing something wrong in the Ajax/JQuery I think. </p>
  
  <p>Please see relevant code below:  </p>
  
  <p>//html </p>
  
  <pre><code>            &lt;form asp-controller="Home" asp-action="Download" method="get"&gt;
                  &lt;div id="downloadButton"&gt;&lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>//JQuery datagrid on my view, which calls the getLoadId method when user selects data row: </p>
  
  <pre><code>    $("#gridContainer2").dxDataGrid({
          dataSource: DevExpress.data.AspNet.createStore({
              loadUrl: url + "/GetLoadHistory",
              key: "loadId"
  
          }),
          selection: {
              mode: "single"
          },
          hoverStateEnabled: true,
          paging: {
              pageSize: 10
          },
  
          editing: {
              mode: 'row',
  
          },
          onSelectionChanged: getLoadId
      });
  </code></pre>
  
  <p>//JQuery functions with Ajax which passes selected loadId to controller: </p>
  
  <pre><code>function getLoadId() {
      var dataGrid = $("#gridContainer2").dxDataGrid("instance");
      var loadIds = dataGrid.getSelectedRowKeys();
      var loadId = loadIds[0];
  
      if (loadId &gt; 0)
      {
          console.log("Load ID: " + loadId);
          download(loadId);
  
      }
  }
  
  function download(loadId) {
      $("#downloadButton").dxButton({
          text: "Download Selected",
          type: "default",
          icon: "/favicon.ico",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          useSubmitBehavior: true,
          onClick: function(e) {
              $.ajax({
                  url: 'https://localhost:44346/home/Download/',
                  type: 'GET',
                  data: {
                      loadId: loadId
                  }
              }).done(function() {
                  DevExpress.ui.notify("Downloading results for Load ID: " + loadId, "info", 1500);
              });
          }
      });
  
      console.log("Load ID: " + loadId + " download processed");
  
  };
  </code></pre>
  
  <p>//download methods on home controller: </p>
  
  <pre><code>    [HttpGet]
      public async Task&lt;IActionResult&gt; Download(int loadId)
      {
  
          Console.WriteLine("Attempting to download Load ID: " + loadId + " from UI");
  
          var outputFolder = Directory.GetCurrentDirectory() + "\\wwwroot\\output\\";
          var name = GetFileName(loadId, outputFolder);
          var path = Path.Combine(outputFolder, name);
  
          var memory = new MemoryStream();
  
          try
          {
              using (var stream = new FileStream(path, FileMode.Open))
              { 
                  await stream.CopyToAsync(memory);
              }
  
              memory.Position = 0;
          }
  
          catch (Exception e)
          {
              Log.Error($"Unable to copy load {loadId} to memory for download: " + e);
          }
  
          Log.Info($"Results for file load: " + loadId + " download triggered successfully");
  
          return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Downloaded.xlsx");
      }
  
  
      public string GetFileName(int loadId, string outputFolder)
      {
  
          Log.Info($"Locating results for load {loadId} on web server");
  
          var resultsName = @"Results_Load_" + loadId + "_*";
          var results = "";
          var d = new DirectoryInfo(outputFolder);
  
          try
          {
              foreach (var file in d.GetFiles(resultsName))
              {
                  Log.Info($"Results for load {loadId} located for download");
                  results = file.Name;
              }
          }
  
          catch (Exception e)
          {
              Log.Error($"Results for load {loadId} not found on server: " + e);
              return null; 
          }
  
          return results; 
  
      }
  </code></pre>