为什么 Firefox 中的会话 ID 会发生变化

发布于 2024-10-23 21:56:20 字数 147 浏览 5 评论 0原文

我正在使用 uploadify 上传文件,我想保存文件,并且想将路径保存在数据库中,因此我在会话中以及用户提交表单后保存路径。它可以在 Internet Explorer 上运行,但在 Firefox 上无法运行,因为会话 ID 发生了更改。

如何解决这个问题呢?

I am using uploadify to upload my files and I want to save the files and I want to save path in database, so I am saving the path in session and after the user submit the form. It works on Internet Explorer but on Firefox it's not working because of the change of the session Id.

How to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-10-30 21:56:20

uploadify 插件不会发送 cookie,因此服务器无法识别会话。实现此目的的一种可能方法是使用 scriptData 参数包含 sessionId 作为请求参数:

<script type="text/javascript">
    $(function () {
        $('#file').uploadify({
            uploader: '<%= Url.Content("~/Scripts/jquery.uploadify-v2.1.4/uploadify.swf") %>',
            script: '<%= Url.Action("Index") %>',
            folder: '/uploads',
            scriptData: { ASPSESSID: '<%= Session.SessionID %>' },
            auto: true
        });
    });
</script>

<% using (Html.BeginForm()) { %>
    <input id="file" name="file" type="file" />
    <input type="submit" value="Upload" />
<% } %>

这会将 ASPSESSID 参数与文件一起添加到请求中。接下来我们需要在服务器上重建会话。这可以在 Global.asax 中的 Application_BeginRequest 方法中完成:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string sessionParamName = "ASPSESSID";
    string sessionCookieName = "ASP.NET_SessionId";

    if (HttpContext.Current.Request[sessionParamName] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[sessionCookieName];
        if (null == cookie)
        {
            cookie = new HttpCookie(sessionCookieName);
        }
        cookie.Value = HttpContext.Current.Request[sessionParamName];
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
}

最后,将接收上传的控制器操作可以使用会话:

[HttpPost]
public ActionResult Index(HttpPostedFileBase fileData)
{
    // You could use the session here
    var foo = Session["foo"] as string;
    return View();
}

The uploadify plugin doesn't send cookies so the server cannot identify the session. One possible way to achieve this is to use the scriptData parameter to include the sessionId as request parameter:

<script type="text/javascript">
    $(function () {
        $('#file').uploadify({
            uploader: '<%= Url.Content("~/Scripts/jquery.uploadify-v2.1.4/uploadify.swf") %>',
            script: '<%= Url.Action("Index") %>',
            folder: '/uploads',
            scriptData: { ASPSESSID: '<%= Session.SessionID %>' },
            auto: true
        });
    });
</script>

<% using (Html.BeginForm()) { %>
    <input id="file" name="file" type="file" />
    <input type="submit" value="Upload" />
<% } %>

This will add the ASPSESSID parameter to the request along with the file. Next we need to reconstruct the session on the server. This could be done in the Application_BeginRequest method in Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string sessionParamName = "ASPSESSID";
    string sessionCookieName = "ASP.NET_SessionId";

    if (HttpContext.Current.Request[sessionParamName] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[sessionCookieName];
        if (null == cookie)
        {
            cookie = new HttpCookie(sessionCookieName);
        }
        cookie.Value = HttpContext.Current.Request[sessionParamName];
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
}

and finally the controller action that will receive the upload could use the session:

[HttpPost]
public ActionResult Index(HttpPostedFileBase fileData)
{
    // You could use the session here
    var foo = Session["foo"] as string;
    return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文