jquery - 在$ .get上提示excel文件

时间:2011-05-25 14:46:26

标签: jquery asp.net ajax

Internet Explorer用于在执行Response.Write

后提示用户下载excel文件
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());

当我通过按钮点击事件回到页面时,这是有效的。

我正在使用页面作为服务并执行$.get(),但结果将以HTML格式发回。我没有被提示打开excel文件。如何将提示发送给用户?

$.get('ExcelService.aspx',
                        { batches: input },
                        function (data) {
                            alert(data);//I see HTML
                        });

2 个答案:

答案 0 :(得分:2)

这是一个类似的线程,有类似的问题(“我想做一个异步GET请求,它返回一个带有MIME内容类型的文档,并使它带来浏览器的'Save'对话框。”)

How to specify content-type and content-disposition with $.ajax() GET response

有人提供了解决方法:

如果您想以编程方式弹出保存对话框,可以使用jQuery将隐藏的iframe附加到页面,其URL为src。这应该根据需要弹出对话框。

<小时/> 的样品
jquery - 点击(不需要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>

答案 1 :(得分:0)

您可以尝试使用Ajax帖子

$.ajax({        dataType: "HTML",
                cache: false,
                type: "GET",
                url: 'ExcelService.aspx';

这将提示get并且比使用jquery

更具灵活性