I'm trying to generate and download Excel file using ASP.NET static WebMethod, which is called via Ajax from jQuery.
[WebMethod]
public static void ExportToExcel(List<ResultGroupItem> searchResult)
{
HttpResponse response = HttpContext.Current.Response;
ExcelExporter excelExporter = new ExcelExporter();
excelExporter.ExportToExcel(searchResult, response);
}
In my ExportToExcel method I generate the xls file and then I add http headers to download the file.
// excelFileBytes is a byte array holding the data to be written
if (excelFileBytes != null)
{
response.Clear();
response.ContentType = "application/octet-stream";
response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", fileName));
response.Flush();
response.BinaryWrite(excelFileBytes);
response.Flush();
response.End();
}
I know that my code is working, but calling it from Ajax did not initiate the download. Here is the response I get (by FireBug in Mozilla Firefox):
How to initiate file download from Ajax?