'ClosedXML SaveAs() Not Returning File

Here is the method inside my controller:

 [HttpGet]
    public ActionResult Export()
    {

        //var svc = new Misc();
        var dt = new DataTable();
        
        dt.Columns.Add("Name");
        dt.Columns.Add("Date");
        dt.TableName = "New";
        using (XLWorkbook wb = new XLWorkbook())
        {
            var ws = wb.AddWorksheet(dt);
            wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            wb.Style.Font.Bold = true;
            ws.Columns().AdjustToContents();
            ws.Rows().AdjustToContents();


            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename= Machine_Report.xlsx");
            using (MemoryStream ms = new MemoryStream())
            {
                wb.SaveAs(ms);
                ms.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();
            }
        }
        return new HttpStatusCodeResult(204);


    }

And here is my view:

         $('#exportList').on('click', function () {
            var filter = $("#filterItem").val();
           
            $.get("/Home/Export",
                {
                    filterItem:filter
                }
            )
            return;
        });

The button click is read and hits the Export method which runs to completion. Nothing returns any errors, however no file is created. Using window.location = url instead of $.get works, but this leaves me no way to pass a value into the Export method. I've also tried $.post which resulted in the same issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source