How to Create and Download File with Ajax in ASP.NET MVC
DRANK

IntroductionWhen you use the Ajax call in ASP.NET MVC, you can just return a JSON object but not a file. If you want to do that, you need to create and save the file in server and return its path to Ajax.After that, you can call a redirect link for downloading the file, because this is a temp file, so you should need to delete it after download.Using the CodeThe below demo code is just for creating and downloading an Excel file:Create an Action for generating the Excel:[HttpPost] public JsonResult ExportExcel() { DataTable dt = DataService.GetData(); var fileName = "Excel_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xls"; string fullPath = Path.Combine(Server.MapPath("~/temp"), fileName); using (var exportData = new MemoryStream()) { Utility.WriteDataTableToExcel(dt, ".xls", exportData); FileStream file = new FileStream(fullPath, FileMode.Create, FileAccess.Write); exportData.WriteTo(file); file.Close(); …

codeproject.com
Related Topics: JavaScript Windows Excel