【问题标题】:Response.AddHeader attachment doesn't workResponse.AddHeader 附件不起作用
【发布时间】:2012-04-12 09:33:18
【问题描述】:

这是我的代码:

                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.BinaryWrite(pck.GetAsByteArray());
                Response.End();

问题是当这段代码运行时(点击按钮)我没有在我的浏览器中下载文件(在 Chrome \ IE 中尝试过)。

pck 是一个 excel 文件(使用 epplus 库生成)。 我什至不知道如何调试这部分代码。它什么也没做。

这是我在浏览器中遇到的错误:

未捕获的 Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException:消息 从服务器收到的无法解析。造成这种情况的常见原因 错误是通过调用 Response.Write() 修改响应时, 响应过滤器、HttpModules 或服务器跟踪已启用。

详细信息:在“PKX��@ϖ�”附近解析出错。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我相信您正在使用更新面板。执行异步回发时无法下载文件。添加将下载文件的按钮作为更新面板的回发触发器。

    【讨论】:

    • 我认为 ramesh 可能是对的。更新面板以管道分隔格式进行通信,其中包括有关要更新的控件、要更新的内容等信息。当您修改响应时,客户端更新面板相关代码无法解析响应,并且您会收到与您类似的错误已发布。
    • 是的,看起来这就是问题所在。我可以通过注册回发控件来修复它:ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.button1);
    【解决方案2】:

    你在正确的轨道上。我认为您缺少几行:

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.Flush();
    Response.Close();
    Response.End();
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多