title: Return excel file response in ASP.NET MVC
body: <p>I am building a FileResult method which is supposed to return an excel file as a result.
  Currently the method looks like this:</p>
  
  <pre><code>public ActionResult GetExcelFile(int id)
      {
          var entityPermission = AuthenticationManager.GetEntityPermission(PermissionEntity.Events, PermissionLevel.AllRecords);
          if (entityPermission == null || !entityPermission.AllowRead)
              return Json(new RequestResult(RequestCode.Unauthorized), JsonRequestBehavior.AllowGet);
  
          try
          {
              using (var serviceManager = new ServiceManager())
              {
                  // Get object model
                  var firstTableObj = serviceManager.GetDataForExcel(id);
                  StringBuilder sb = new StringBuilder();
                  sb.Append("&lt;table border=`" + "1px" + "`b&gt;");
                  sb.Append("&lt;tr&gt;");
                  // ... appending other strings and data
                  sb.Append("&lt;/table&gt;");
  
                  // Return FileResult
                  byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
                  return File(new MemoryStream(byteArray, 0, byteArray.Length), "application/octet-stream", $"{firstTableObj.CurrentDate}.xlsx");                   
              }
          }
          catch (Exception ex)
          {
              return Json(new RequestResult(RequestCode.Server_Failure, ex.Message), JsonRequestBehavior.AllowGet);
          }
  </code></pre>
  
  <p>Currently, when I send a request from Angular 2+ frontend, the request is being processed and returned, this is some of the header data: </p>
  
  <pre><code>Content-Disposition:attachment; filename=12.11.2017.xlsx, Content-Length:617, Content-Type:application/octet-stream
  </code></pre>
  
  <p>However, the file is not being downloaded, the response body is just the html template in string format. What am I missing? I have seen some examples of returning excel files as a FileResult in MVC and they are not much different from mine. Changing the MIME to 'application/vnd.ms-excel' didn't help either. Also, as far as I am aware, there is no need to implement any file download logic in the client, it should work as is, that is a response from the server.
  Will appreciate any hints on the subject.</p>
  
  <p>P.S: I know that in general I should not load an entire file into memory, but this is currently for testing purposes (I know an approximate limit to returned file sizes) and will most surely be changed in the future development.</p>