抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

问题说明

asp.net 项目中有一个上传图片的地方使用的 uploadify 上传插件,而且采用的 FLASH 版本,该功能有一阵子没使用过了,等现在要用的时候发现一个严重问题,提交上传后直接导致项目 Session 丢失,查看代码后发现调用 uploadify 后会创建一个新的 Session ,下面是解决办法。

前台 js 代码

$(function () {
            var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
            var ASPSESSID = "<%= Session.SessionID %>";
            uploader = new AjaxUploaderBtn(document.getElementById("uploader")); 
            uploader.init({
                fileTypeExts: "*.gif; *.jpg; *.png; *.jpeg;",
                onSWFReady: function () {
                        $(uploader.btn).uploadify("disable", true);
                },
                onUploadStart: function (file) {
                    $(uploader.btn).uploadify("settings", "formData", { 'lang': "en", ASPSESSID: ASPSESSID, AUTHID: auth });
                },
                onUploadSuccess: function (file, data, response) {
                    var json = JSON.parse(data);                    
                    if (response && json.Success) {
                        console.log(json);
                    }
                    else if (false == json.Success) {
                        console.log(json.message);
                    }
                    else {
                        alert("Request error.");
                    }
                },
                onUploadError: function (file, errorCode, errorMsg, errorString) {
                    alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                }
            });
        });

AjaxUploaderBtn 是封装的一个 js 函数,这里的重点代码是:

  1. 声明两个变量

    var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
    var ASPSESSID = "<%= Session.SessionID %>";
  2. 传递变量

    formData 中传递这两个变量

    $(uploader.btn).uploadify("settings", "formData", { ASPSESSID: ASPSESSID, AUTHID: auth });

后台处理

打开 Global.asax 文件

  1. 添加一个函数

    private void UpdateCookie(string cookie_name, string cookie_value)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
        if (null == cookie)
        {
            cookie = new HttpCookie(cookie_name);
        }
        cookie.Value = cookie_value;
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
  2. 修改 Application_BeginRequest 方法

    protected void Application_BeginRequest (object sender, EventArgs e)
    {
        try
        {
            string session_param_name = "ASPSESSID";
            string session_cookie_name = "ASP.NET_SessionId";
    
            if (HttpContext.Current.Request.Form[session_param_name] != null)
            {
                UpdateCookie (session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
            {
                UpdateCookie (session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
            }
        }
        catch { }
    
        try
        {
            string auth_param_name = "AUTHID";
            string auth_cookie_name = FormsAuthentication.FormsCookieName;
    
            if (HttpContext.Current.Request.Form[auth_param_name] != null)
            {
                UpdateCookie (auth_cookie_name,
                              HttpContext.Current.Request.Form[auth_param_name]);
            }
            else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
            {
                UpdateCookie (auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
            }
    
        }
        catch { }
    }

评论