0

i am using asp.net mvc webapi and ajax in my project

i am calling controller using ajax and sending data with ajax

here is my ajax code

var bobj = {};
bobj.data = data;
CallAjax('post',common,'attendancesheetdownload',bobj,_getholidaylist.onsuccestendancesheetdownload, '');

    function CallAjax(method, baseUrl, strUrl, strData, onSuccess, onFailure) {
        callDebugger();
        var dataValue = null;
        var ContentType = "application/json; charset=utf-8";
        strUrl = baseUrl + strUrl;
        method = method.toUpperCase();
        if (method == "GET") {
            dataValue = strData;
        }
        else if (method == "POST") {
            dataValue = JSON.stringify(strData);
        }
        callLoader(true);
        $.ajax({
            type: method,
            url: strUrl,//users/signin
            contentType: ContentType,
            dataType: 'json',
            data: dataValue,
            async: false,
            success: onSuccess,
            error: function (err) {
                callLoader(false);
                swal({
                    title:err, text: "", type: "error",
                    showCancelButton: false, closeOnConfirm: true, confirmButtonText: "OK",
                }, function (isConfirm) {
                    window.location = "signin";
                });
                 //console.log(err);
            }
        });
    }

and here is my c# controller code for excel generate

[HttpPost]
[Route("attendancesheetdownload")]
public JsonResult AttendanceSheetDownload(List<AttendanceModel> data)
{
    //List<AttendanceModel> holidaylist = new List<AttendanceModel>();
    try
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        using (XLWorkbook wb = new XLWorkbook())
        {


            //DataTable dt = new DataTable();

            dt.Columns.Add("Employee Name");
            dt.Columns.Add("Leaves");
  //          dt.Columns.Add("URL");
            foreach (var item in data)
            {
                var row = dt.NewRow();

                row["Employee Name"] = item.user_id;
                row["Leaves"] = Convert.ToString(item.days);
              //  row["URL"] = item.URL;

                dt.Rows.Add(row);
            }
           // dt = attendacemodel;
            wb.Worksheets.Add(dt, "CusDetail");
            wb.ShowRowColHeaders = false;
            wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            wb.Style.Font.Bold = true;
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=Customers.xlsx");
            using (MemoryStream MyMemoryStream = new MemoryStream())
            {
                wb.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();

            }
        }
        return Json(data);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        data = null;
    }
}

i dont see any error at c# side it easly pass all the step but the file is not downloading and when its comes to onsuccess method of ajax its give me object object error

can some one guide me how to download excel file with ajax c# and webapi

Chitra Nandpal
  • 413
  • 4
  • 24

0 Answers0