jquery - prompt for excel file on $.get
Internet explorer used to prompt a user to download an excel file after doing a Response.Write
Response.ContentType = "application/vnd.ms-excel";
Response.AddHea开发者_如何学Goder("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)
var dynamicUrl = 'ExcelService.aspx?batches=' + input;
$('#excelPopup').attr('src', dynamicUrl);
window.frames["#excelPopup"].location.reload();
HTML
<iframe id="excelPopup" style="visibility:hidden;height:0px;width:0px;"></iframe>
You could try an Ajax post
$.ajax({ dataType: "HTML",
cache: false,
type: "GET",
url: 'ExcelService.aspx';
this will prompt the get and has a bit more flexibility than just using jquery
精彩评论