我在我的项目中使用asp.net mvc webapi和ajax
我使用ajax调用控制器并使用ajax发送数据
这是我的ajax代码
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);
}
});
}
这是我的用于excel generate的c#控制器代码
[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;
}
}
我没有在c#侧看到任何错误,它很容易通过所有步骤,但文件没有下载
和
当它来到ajax的onsuccess方法时,它给我object object
错误
有人可以指导我如何使用ajax c#和webapi
下载excel文件