用于提示用户做后下载Excel文件Internet Explorer中的Response.Write
Response.ContentType =应用程序/ vnd.ms-EXCEL;
Response.AddHeader(内容处置,附件;文件名= \sheet.xls \);
Response.RedirectLocation =export.xls;
Response.Charset的=;
的EnableViewState = FALSE;
System.IO.StringWriter oStringWriter =新System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter =新System.Web.UI.HtmlTextWriter(oStringWriter);
dataGridResult.RenderControl(oHtmlTextWriter);
回复于(oStringWriter.ToString());
这工作时,我回发到一个网页,一个按钮单击事件。
我使用的是页面服务,做一个 $。获得()
,但结果发回的HTML。我不提示打开Excel文件。如何发送提示出用户?
$。获得(ExcelService.aspx',
{批次:输入},
功能(数据){
警报(数据); //我看到HTML
});
解决方案
这是一个类似线,以我想作一个返回回来MIME内容类型的文档异步GET请求,并导致它类似的问题(把浏览器的保存对话框。)
[jquery]如何指定内容类型的AMD内容处置与$。阿贾克斯()得到响应
有人有提供解决方法吧:
如果您希望以编程方式弹出一个保存对话框,你可以使用jQuery追加一个隐藏的iframe的页面的URL,因为它的src。这应该在必要时弹出对话框。
样品 jQuery的 - 对点击(不需要AJAX / GET)
VAR dynamicUrl ='ExcelService.aspx批=?+输入;
$('#excelPopup)ATTR(SRC,dynamicUrl)。
window.frames [#excelPopup] location.reload()。
HTML
< IFRAME ID =excelPopup的风格=能见度:隐藏;高度:0px;宽度:0px;>< / IFRAME>
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.")
[jquery] How to specify content-type amd 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>