【问题标题】:Export to Excel code doesn't trigger download导出到 Excel 代码不会触发下载
【发布时间】:2013-07-06 07:20:26
【问题描述】:

我尝试使用以下 ASP.NET MVC 4 代码导出表(由 jTable jquery 插件生成,但它只是一个表,因此无关紧要)。它不会触发下载文件对话框。

另一个问题是虽然 HTML 编码表是由 .ajax() POST 发送的,但当我尝试使用 string inp = Request["input"]; 处理值时会触发错误。我在 [HttpPost] 之前和之后都使用了[ValidateInput(false)] 声明来克服“从客户端检测到潜在危险的 request.form 值”错误(因为我正在发送 HTML 表)但该声明没有防止抛出“潜在危险的 request.form 值...”异常 -

我的 .cshtml 页面中的代码

 $("#exportToExcel").on("click", function (e) {
                $.ajax({
                    url: '/Reports/Export',
                    cache: false,
                    dataType: "html",
                    type: 'POST',
                    data: { input: $(".jtable").html() },
                    success: function (data) {

                    },
                    complete: function () {

                    }
                });
            })

报告控制器中的代码:

[ValidateInput(false)]
[HttpPost]
public void Export()
{
    //string inp = Request["input"];
    string body = "<table><tr><td>2</td><td>3</td></tr><table>";
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=data.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    //Response.ContentType = "application/vnd.ms-excel";
    Response.ContentType = "application/force-download";

    Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
    Response.Write("<head>");
    Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
    Response.Write("<!--[if gte mso 9]><xml>");
    Response.Write("<x:ExcelWorkbook>");
    Response.Write("<x:ExcelWorksheets>");
    Response.Write("<x:ExcelWorksheet>");
    Response.Write("<x:Name>Report Data</x:Name>");
    Response.Write("<x:WorksheetOptions>");
    Response.Write("<x:Print>");
    Response.Write("<x:ValidPrinterInfo/>");
    Response.Write("</x:Print>");
    Response.Write("</x:WorksheetOptions>");
    Response.Write("</x:ExcelWorksheet>");
    Response.Write("</x:ExcelWorksheets>");
    Response.Write("</x:ExcelWorkbook>");
    Response.Write("</xml>");
    Response.Write("<![endif]--> ");
    Response.Write("<![endif]--> ");
    Response.Write("</head>");
    Response.Write("<body>");
    Response.Write(body);
    //Response.Write(inp);
    Response.Write("</body>");
    Response.Write("</html>");
    Response.End();

}

【问题讨论】:

    标签: c# jquery asp.net-mvc-4


    【解决方案1】:

    无法通过ajax调用实现文件下载响应,实现这一点的方法很少。以下是其中之一,

    $("#exportToExcel").on("click", function (e) {
        var form = $('form');
        var input = $('<input type="hidden" name="input" value="" />');
        input.val($(".jtable").html());
        form.append(input);
        form.attr('action', '/Reports/Export');
        form.attr('method', 'POST');
        form.submit();
    })
    

    希望这会有所帮助。

    【讨论】:

    • 感谢@shakib。这个信息对我有用。我的主要问题是触发下载。即使使用虚拟表,控制器导出方法代码也不起作用。一旦我开始工作,我就可以使用您的代码发送真实的表格内容。
    • 上述代码将触发下载。现在您只需让Export 操作使用正确的数据即可。 P.S 在此方法中,您将在操作参数 public void Export(string input) 或 formcollection public void Export(FormCollection data){ var input = data["input"]; } 中找到输入
    • 如果我有多个表单,我如何才能只针对我正在发布值的动态表单
    • 没看懂你想说什么,但是要发布的表单在DOM结构中不存在,它只是为了发布一个包含jtable值的输入字段。
    • 我在网页中有 2 个表单。当我通过 IE 开发工具查看 HTTP 响应正文(详细视图中的网络选项第 2 个选项卡响应正文)时,我看到的不仅是“input=%3Cthead .....”,而是之前表单中的一个字符串。跨度>
    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 2013-09-25
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2016-05-26
    相关资源
    最近更新 更多