1

这是我的代码:

                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��@ϖ�”附近解析错误。

4

2 回答 2

2

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

于 2012-04-12T01:33:18.707 回答
0

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

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();
于 2012-04-12T01:30:21.853 回答