Hello here my code of my controller action:
public ActionResult ExportExcel()
{
DataTable dt = new DataTable();
FILL DataTable
try
{
FileExcel(dt);
return Json(new { successCode = "1" });
}
catch (Exception ex)
{
return Json(new { successCode = "0" });
}
}
public void FileExcel(DataTable dt)
{
GridView gv = new GridView();
gv.DataSource = dt;
gv.DataBind();
HttpContext context = System.Web.HttpContext.Current;
context.Response.ClearContent();
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition", "attachment; filename=Ricerca.xls");
context.Response.ContentType = "application/ms-excel";
context.Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
context.Response.Output.Write(sw.ToString());
context.Response.Flush();
htw.Close();
sw.Close();
context.Response.End();
}
My Ajax Jquery function
function ExportExcel() {
$.ajax({
type: "POST",
url: '/Home/ExportExcel/',
beforeSend: function () {
AjaxStart('...Export');
},
success: function (data, textStatus, jqXHR) {
AjaxStop();
alert('success')
},
error: function (failure) {
alert('error');
}
});
}
if i call ajax function ExportExcel() through a simple link as
<a href="#" onclick="ExportExcel();">Export Excel Ajax</a>
it doesn't works !!!!!!
while if use a simple controller action as
@Html.ActionLink("Export Excel", "ExportExcel", "Home")
the excel file to save appears! i would like use first method (ajax) to manage the esporting delay