title: asp.net core download excel file and messages
body: <p>I need to call an Action Result to generate and download an Excel file, but I'd like to show a message if something goes wrong. This is ASP.NET Core 2</p>
  
  <p>The generation part it is not a problem, and also the download, but I do have problems on how to call that action and show a message when there is an error.</p>
  
  <p>To download the file I usually do:</p>
  
  <pre><code>[HttpGet]
  public IActionResult MyFileDownload(int id)
  {
      if (id == 0) MESSAGE;
  
      var filePath = GenerateAndSaveExcel(id);
      if (string.IsNullOrEmpty(filePath)) MESSAGE;
  
      using (var webClient = new System.Net.WebClient())
      {
          return File(new MemoryStream(webClient.DownloadData(filePath)), "application/pdf", "myfile.xlsx");
      }
  }
  </code></pre>
  
  <p>The GenerateAndSaveExcel function just creates a new Excel file using ClosedXml (if someone is interested I can share what I do) and returns the path of the new file.</p>
  
  <p>I call the function like this:</p>
  
  <pre><code>&lt;a asp-action="MyFileDownload" asp-controller="myController"&gt;download&lt;/a&gt;
  </code></pre>
  
  <p>But I'd like to change the way I call the function because if id=0 or something goes wrong in the generate function I'd like to send message, usually I use son for that and show it using javascript.</p>
  
  <p>It could so simple, but I don't see the way of doing this.
  Maybe an Ajax call could solve it, but I don't know how to manage the download. </p>
  
  <p>Another thing is, I like the download this way, so the file goes directñly to the download folder.</p>
  
  <p>Thanks.</p>