The solution that I currently have so that it works for me uses windows.location, I want with ajax to obtain a response and thus implement a loading that is seen when the download starts and disappears when it finishes with an alert message.
You will see how he creates a ExcelResultwhere he assigns the Responseassignee ContentTypeand you would also pass the render of the excel that you want to send
public class ExcelResult : ActionResult
{
public string XMLStream { get; set; }
public string FileName { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Buffer = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
context.HttpContext.Response.ContentType = "application/vnd.ms-excel";
context.HttpContext.Response.Write(XMLStream);
}
}
Regarding ajax, you can't download files via ajax . There are libraries that simulate the effect of downloading files with ajax such as jquery.fileDownload
If you are implementing webapi you will have to return a FileResult or create a custom ActionResult
Export To Excel in MVC
You will see how he creates a
ExcelResult
where he assigns theResponse
assigneeContentType
and you would also pass the render of the excel that you want to sendRegarding ajax, you can't download files via ajax . There are libraries that simulate the effect of downloading files with ajax such as jquery.fileDownload