Internet explorer used to prompt a user to download an excel file after doing a Response.Write
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=\"sheet.xls\"");
Response.RedirectLocation = "export.xls";
Response.Charset = "";
EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dataGridResult.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
This works when I POST back to a page with a button click event.
I am using a page as a service and doing a $.get(), but the results are sent back as HTML. I am not prompted to open the excel file. How can send the prompt out to the user?
$.get('ExcelService.aspx',
{ batches: input },
function (data) {
alert(data);//I see HTML
});
This is a similar thread, with a similar problem (“I would like to make an async GET request that returns back a document with MIME content type and cause it to bring the browser’s ‘Save’ dialog.”)
How to specify content-type and content-disposition with $.ajax() GET response
Someone there offers a workaround to it:
If you’d like to programatically pop a save dialog box, you can use jQuery to append a hidden iframe to the page with the URL as it’s src. This should pop the dialog box as necessary.
SAMPLE
jquery – on click (dont need ajax/get)
HTML