zoukankan      html  css  js  c++  java
  • 解决文件上传插件Uploadify在火狐浏览器下,Session丢失的问题

    因为在火狐浏览器下Flash发送的请求不会带有cookie,所以导致后台的session失效。

    解决的方法就是手动传递SessionID到后台。

     $("#fileresultfiles").uploadify({
                    swf: '/Scripts/uploadify/uploadify.swf',
                    uploader: '/UploadFiles.ashx',
                    queueID: 'fileQueue',
                    buttonText: '附件上传',
                    auto: true,
                    debug: false,
                    removeCompleted: true,
                    multi: true,
                    displayData: 'speed',
                    uploadLimit: 20,
                    fileSizeLimit: '500MB',
                    formData: {
                        'ASPSESSID': '<%=Session.SessionID %>',
                        'primarykey': $("#<%=hid_resultStrId.ClientID %>").val(),
                    },
                    onQueueComplete: function (queueData) {
                        //队列上传完成后,ajax获取上传的所有列表
                        AjaxRequestFile();
                    },
                    onSelectError: function (file, errorCode, errorMsg) {
                        var errorMsg = "";
                        if (errorCode == "QUEUE_LIMIT_EXCEEDED") {
                            errorMsg = "文件数量过多!";
                        }
                        if (errorCode == "-110") {
                            errorMsg = "文件大小超出限制!";
                        }
                        $('#' + file.id).find('.data').html(' errorMsg');
                    },
                    onUploadSuccess: function (file, data, response) {
                        if (data != "OK") {
                            $("#fileresultfiles").uploadify('cancel', '*');
                            alert(data);
                        }
                    }
                });
    

     'ASPSESSID': '<%=Session.SessionID %>'这句就是把SessionID传递过去

    然后在Global.asax文件中监听每一个请求,如果有SessionID传过来就重新设置cookie

            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                //为了Uploadify在谷歌和火狐下不能上传的BUG
                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
                {
                }
            }
    
            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);//重新设定请求中的cookie值,将服务器端的session值赋值给它
                }
            }
    
  • 相关阅读:
    迷你资源管理器
    简单工厂和单例的一些事
    面向对象的七大原则
    继承和多态的一些事
    体检套餐系统
    信仰
    魔兽争霸系统
    优化MySchool总结习题
    事务,视图及索引
    [LeetCode] Combinations
  • 原文地址:https://www.cnblogs.com/haomo/p/5580592.html
Copyright © 2011-2022 走看看