0

Attempt:

    // see http://www.aspsnippets.com/Articles/Create-and-download-Excel-file-in-ASPNet.aspx
    [HttpPost]
    protected void FullDump ( object sender , EventArgs e )
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SurveyDbStaging"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Answers"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        using (XLWorkbook wb = new XLWorkbook())
                        {
                            wb.Worksheets.Add(dt, "Answers");

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

called in the view by

    $('#data-dump-button').click(function(){
        $.ajax({
            url : 'Excel/FullDump',
            method : 'POST',
            success: function () {
                console.log("The success function was called!");
            }
        });
    });

This just gives me

http://thesite.com/Excel/FullDump 404 (NOT FOUND)

in the console. What should I be doing instead?

Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
  • See http://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc – NikolaiDante Feb 22 '16 at 21:04
  • the above comment will work for you to get the file saved on the server and downloaded, but wont help you with your writing to the file. Take the response out of the using statement and replace it in the code after the using statement – Simon Price Feb 22 '16 at 21:51
  • @SimonPrice Can you be a little more specific on how I am to cut-n-paste my current code? – Subpar Web Dev Feb 22 '16 at 22:04

0 Answers0