0

I use MVC 4 / C#. (VS 2013) I don't know how to solve the problem with export to excel with ajax call. Does anyone has some working example on this ? Please, if you have working example. Here's the code which I couldn't make it work with ajax call:

var exportXLSDeadlinesParent = function () {
        if ($('#hiddenParentCount').val() > 0) {
            $.ajax({
                url: '../../Administration/UserManagement/exportUserProp',
                type: 'POST',
                data: 'typeToExport=PARENT',                
                success: function (data) {
                },
                error: function (xhr, err) {
                    alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    alert("responseText: " + xhr.responseText);
                }
            });
        }
    };

And here is controller action:

 [HttpPost]
        public virtual void exportUserProp(string typeToExport, string validFrom = null)
        {
            var query = ModelPropSessions:           

            using (ExcelPackage pck = new ExcelPackage())
            {
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add(string.Format("{0} - {1}", "historyData", typeToExport));

                ws.Cells[1, 1].Value = "ID user";
                ws.Cells[1, 2].Value = "Agent number";
                ws.Cells[1, 3].Value = "Deadline type";                             

                for (int z = 0; z < query.Count(); z++)
                {                    
                    ws.Cells[z + 2, 1].Value = query[z].USERID;                    
                    ws.Cells[z + 2, 2].Value = query[z].USER_INT_NUM;                   
                    ws.Cells[z + 2, 3].Value = query[z].DESC;                    
                }

                string myFile = "FileToExport";

                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=" + myFile);
                Response.BinaryWrite(pck.GetAsByteArray());
                Response.End();
            }            
        }
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
  • 1
    You asked the same question yesterday. You can't send mime type file through ajax. Why do you need ajax for this? – charlietfl Oct 07 '15 at 14:47
  • Thanks for answer. So, it's not possible. – FrenkyB Oct 07 '15 at 14:55
  • 2
    check the answer here.. it mimics a form post that should work for you http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – JamieD77 Oct 07 '15 at 14:56

0 Answers0