I use a jqQuery UI Dialog that collect parameters which should be posted to an aspx page that will download a pre-generated excel file based on the report parameters.
I already have a ReportGenerator.aspx page which when executed opens the excel file
String filePath ="<path>\Graph_IR_Scenarios.xlsx";
String outputFileName = @"Graph_IR_Scenarios.xlsx";
FileInfo fileInfo = new FileInfo(filePath);
HttpContext context = HttpContext.Current;
HttpResponse response = context.Response;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentType = "vnd.ms-excel";
response.AppendHeader("content-type", "vnd.ms-excel");
response.AppendHeader("content-length", fileInfo.Length.ToString());
response.AppendHeader("content-disposition", String.Format("attachment; filename={0}", outputFileName));
response.TransmitFile(filePath);
response.Flush(); // this make stream and without it open chrome save dialog
context.ApplicationInstance.CompleteRequest(); // send headers and c# server-side code still continue
response.End();
//jQuery Ajax Request
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: {},
success: function (data, textStatus, xhr) {
alert('textStatus : ' + textStatus);
alert('xhr : ' + xhr);
},
error: function (xhr, textStatus, errorThrown) {
alert('textStatus : ' + textStatus);
alert('errorThrown : ' + errorThrown);
}
});
Though the request goes to the server the excel file never gets downloaded though the aspx page if executed directly works