内容处置不会导致另存为 [英] content-disposition does not cause a save as

查看:74
本文介绍了内容处置不会导致另存为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

Response.ClearContent();
Response.AddHeader( "Content-type", "application/vnd.ms-excel");
Response.AddHeader("content-disposition", "attachment;filename=test.xls");
Response.ContentType = "application/excel";
var swr = new StringWriter();
var tw = new HtmlTextWriter(swr);
grd.RenderControl(tw);
Response.Write(swr.ToString());
Response.Flush();
Response.End();
tw.Close();
swr.Close();

此动作是由以下jQuery代码触发的:

This action is triggered from the following bit of jquery code:

   <img src="../../../../Content/images/Excel-icon.png" 
      onclick = "alert ($('#Filter').serialize());
                    $.ajax({
                         type: 'POST',
                         url: '<%=Url.Action( "Excel") %>',
                         data: $('#Filter').serialize(),
                         success : function (data, textStatus, jqXHR) 
                              { alert (data);},
                         error: function (jqXHR, textStatus, errorThrown)
                              { alert (textStatus + ' ' + errorThrown);}
                         });" />

我已与提琴手确认,标题具有预期值.我还可以使用Web View在Fiddler中查看数据.

I have confirmed with fiddler that the headers have the expected values. I can also see the data in fiddler using the Web View.

此外,当我显示来自成功函数的数据内容时,它具有要尝试导出到excel的表的原始html.

Also, when I show the contents of data from the success function, it has the raw html for the table that I am trying to export to excel.

预先感谢您的帮助.

推荐答案

哦,不,您不能使用AJAX下载文件.执行AJAX调用,将文件流式传输到客户端,执行成功回调,并传递文件内容,这将使您陷入困境.出于明显的安全原因,您不能对此数据做太多事情.您无法将其保存到客户端计算机.您目前无法显示另存为"的提示.

Oh, no, you cannot use AJAX to download files. The AJAX call is executed, the file is streamed to the client, your success callback is executed and passed the contents of the file that's where you will get stuck. For obvious security reasons you cannot do much with this data. You cannot save it to the client computer. You cannot show a prompt to Save As at this stage.

因此,请删除您的javascript AJAX调用,并使用普通的GET或POST请求调用您的控制器操作.

So remove your javascript AJAX call and invoke your controller action using a normal GET or POST request.

这篇关于内容处置不会导致另存为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆