代码之家  ›  专栏  ›  技术社区  ›  Chris

文件下载后会话过期

  •  1
  • Chris  · 技术社区  · 17 年前

    当用户从我的ASP.NET应用程序下载文件时,会话将在下载文件几秒钟后过期。在可以执行任何任务的会话到期之前,但在大约5-10秒后,会话将重新启动并注销。

    我创建了一个简单的页面来演示这一点。要运行此简单页,请创建一个新的ASP.NET C项目,然后将代码插入新页。

    编辑: 这似乎是一个IE7问题。火狐和Chrome不受影响。

    我认为负责会话重新启动的代码是:

    HttpContext.Current.Response.ContentType = "text/xml";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
    HttpContext.Current.Response.Write("<test>this is a test.</test>");
    HttpContext.Current.Response.End();
    

    要重新创建此问题:

    1. 将下面的代码复制到ASP.NET页中。
    2. 使用IE(我使用了IE7,火狐和Chrome似乎没有这个问题)
    3. 请注意,会话是新的。
    4. 刷新页面;注意会话不是新的。
    5. 下载并保存文件。
    6. 点击“刷新页面”按钮几次,直到“会话是新的”文本重新显示(大约10秒)。

    以下是简单娱乐活动的代码:

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <script runat="server">
            private string sessionString {
                get {
                    return HttpContext.Current.Session["sessionString"] == null ? null : HttpContext.Current.Session["sessionString"].ToString();
                }
                set {
                    HttpContext.Current.Session["sessionString"] = value;
                }
            }
            protected void Page_Load(object sender, EventArgs e) {
                Label1.Text = sessionString ?? "Session is null";
                if(sessionString == null) {
                    Label1.Text = "Session is new";
                    Label1.BackColor = System.Drawing.Color.Red;
                    sessionString = "Session is now not null";
                }
                else {
                    Label1.Text = sessionString;
                    Label1.BackColor = System.Drawing.Color.White;
                }
            }
            protected void LinkButton1_Click(object sender, EventArgs e) { }
            protected void LinkButton2_Click(object sender, EventArgs e) {
                HttpContext.Current.Response.ContentType = "text/xml";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
                HttpContext.Current.Response.Write("<test>this is a test.</test>");
                HttpContext.Current.Response.End();
            } 
        </script>
        <form id="form1" runat="server">
        <div>
            <asp:Label id="Label1" runat="server" text="Label"></asp:Label> <br />
            <asp:LinkButton id="LinkButton1" runat="server" onclick="LinkButton1_Click">Refresh Page</asp:LinkButton> <br />
            <asp:LinkButton id="LinkButton2" runat="server" onclick="LinkButton2_Click">Download File</asp:LinkButton> <br /><br />
            <b>Steps to recreate:</b>
            <ol>
                <li>Download the file and save it.</li>
                <li>Hit the "Refresh Page" button a couple of times until the "Session is new" text is redisplayed.</li>
                <li>Answer my question explaining what the heck is going on!</li>
            </ol>
        </div>
        </form>
    </body>
    </html>
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   RichardOD    17 年前

    Something to do with this (可能是类似的问题)?也许你可以用小提琴来更详细地观察饼干的情况。

        2
  •  1
  •   Jocko johnson    15 年前

    确保在摆脱response.clear()调用后使用新的浏览器开始。我也遇到了同样的问题,阅读了这篇文章,摆脱了响应。clear()有效,但只是在我把桌面上打开的浏览器扔掉,打开了一个新的实例之后。祝你好运!

        3
  •  0
  •   Tor Haugen    17 年前

    嗯,可以吗 Response.Clear() 吃会话状态cookie?

    推荐文章