title: How to download .xslx File from server using EPPlus and MVC
body: <p>I have an Excel file generated on the server by using EPPlus the file is correctly made and using a window.location works fine on the local machine but does nothing when its deployed to a server. I'm trying to return a FileStreamResult back through the MVC controller but i don't think it's working. i'm using an ajax call to access the controller method but it fails to enter .done when the method is run through.</p>
  
  <p>i have been looking at <a href="https://stackoverflow.com/questions/18728995/asp-net-mvc-epplus-download-excel-file">ASP.NET MVC EPPlus Download Excel File</a> for my C# reference.</p>
  
  <p>Script</p>
  
  <pre><code> function exportToExcel() {
      var batchName = $("#batchDateSelect option:selected").text();
      var bID = $("#batchDateSelect").val();
      var params = {
          BatchID: bID,
          BatchName: batchName
      };
      $.post(path + "Export/ExportToExcel", params)
      .done(function (Data, textStatus, jqXHR) {
          var fileName = "";
  
          ////window.location = path + "ExportFiles/"+fileName;
      });
  
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public ActionResult ExportToExcel(int BatchID,string BatchName)
  {
  
      FileStreamResult FSR = DataAccess.ExportUtility.CreateExcelFile(BatchID, BatchName);
  
      return FSR;
  
  }
  </code></pre>
  
  <p>EPPlus Method</p>
  
  <pre><code>public static FileStreamResult CreateExcelFile(int batchid,string batchName)
      {
  
          string fileName = batchName + " Reason_Code_Export.xlsx";
          var serverPath = HttpContext.Current.Server.MapPath("~/ExportFiles/");
          DirectoryInfo outputDir = new DirectoryInfo(serverPath);
  
          FileInfo newfile = new FileInfo(outputDir.FullName + fileName);
          if (newfile.Exists)
          {
              newfile.Delete();
              newfile = new FileInfo(outputDir.FullName + fileName);
          }
          Dictionary&lt;string,int&gt; MAData = PolicyDataAccess.GetMatchActionData(batchid);
          MemoryStream MS = new MemoryStream();
          using (ExcelPackage package = new ExcelPackage(newfile))
          {
              ..........
              ........
  
              package.SaveAs(MS);
          }
          MS.Position = 0;
          var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  
          FileStreamResult FSR = new FileStreamResult(MS, contentType);
          FSR.FileDownloadName = fileName;
  
  
  
          return FSR;
      }
  </code></pre>
  
  <p>What is the easiest way to Get that file?</p>