JQuery文件上传插件uploadify在MVC中Session丢失的解决方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | <script type= "text/javascript" > var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)" ; var ASPSESSID = "@Session.SessionID" ; $(function () { $( "#file_upload" ).uploadify({ swf: '/Scripts/uploadify/uploadify.swf' , uploader: '/Project/File/FileUpload' , formData: { ASPSESSID: ASPSESSID, AUTHID: auth }, auto: true , … }); 注意:上面灰底代码很重要! 将下面代码放入Global.asax 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 { } } 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); } |
我的uploadify页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | @{ Layout = null ; } <!DOCTYPE html> <html> <head> <link href= "../../Content/uploadify/uploadify.css" rel= "stylesheet" type= "text/css" /> <script src= "../../Scripts/jquery-1.5.1.js" type= "text/javascript" ></script> <script src= "../../Content/uploadify/jquery.uploadify.js" type= "text/javascript" ></script> <script src= "../../js/jquery.easyui.min.js" type= "text/javascript" ></script> <link href= "../../css/themes/default/easyui.css" rel= "stylesheet" type= "text/css" /> <style type= "text/css" > * { font-family: "microsoft yahei" , "Times New Roman" , "宋体" , Times, serif; } </style> <script type= "text/javascript" > var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)" ; var ASPSESSID = "@Session.SessionID" ; $(function () { $( '#uploadify' ).uploadify({ // debug: false, //开启调试 // successTimeout: 99999, //超时时间 formData: { //附带值 'userid' : '用户id' , ASPSESSID: ASPSESSID, AUTHID: auth }, // queueID: 'some_file_queue', //文件选择后的容器ID uploader: '/User/UploadUserImg' , // 服务器端处理地址 swf: '../../Content/uploadify/uploadify.swf' , // 上传使用的 Flash width: 130, // 按钮的宽度 height: 25, // 按钮的高度 buttonText: "请选择上传的文件" , // 按钮上的文字 buttonCursor: 'hand' , // 按钮的鼠标图标 fileObjName: 'Filedata' , // 上传参数名称 // 两个配套使用 fileTypeExts: "*.jpg;*.png" , // 扩展名 fileTypeDesc: "请选择 jpg png 文件" , // 文件说明 auto: false , // 选择之后,自动开始上传 multi: false , // 是否支持同时上传多个文件 queueSizeLimit: 1, //设置上传队列中同时允许的上传文件数量 允许多文件上传的时候,同时上传文件的个数 uploadLimit: 1, //uploadLimit:设置允许上传的文件数量,默认为999。 // removeCompleted: false, //设置已完成上传的文件是否从队列中移除,默认为true onQueueComplete: function (queueData) { //队列中全部文件上传完成时触发事件。 alert(queueData.uploadsSuccessful + '个文件上传完成,确认后刷新页面' + queueData.uploadsErrored); window.parent.afterUpdateImg(); }, //返回一个错误,选择文件的时候触发 onSelectError: function (file, errorCode, errorMsg) { alert( "error" ); switch (errorCode) { case -100: alert( "上传的文件数量已经超出系统限制的" + $( '#uploadify' ).uploadify( 'settings' , 'queueSizeLimit' ) + "个文件!" ); break ; case -110: alert( "文件 [" + file.name + "] 大小超出系统限制的" + $( '#uploadify' ).uploadify( 'settings' , 'fileSizeLimit' ) + "大小!" ); break ; case -120: alert( "文件 [" + file.name + "] 大小异常!" ); break ; case -130: alert( "文件 [" + file.name + "] 类型不正确!" ); break ; } }, onFallback: function () { alert( "您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。" ); //检测FLASH失败调用 }, onUploadSuccess: function (file, data, response) { //上传到服务器,服务器返回相应信息到data里 } }); }); </script> <title>UploadUserImg</title> </head> <body> <div> <span id= "uploadify" ></span> </div> <div> <button onclick= "javascript:$('#uploadify').uploadify('upload','*');" > 上传图片</button> <button onclick= "javascript:$('#uploadify').uploadify('cancel', '*');" > 取消上传</button> </div> </body> </html> |
我的global
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Text; using System.IO; using System.Web.Security; namespace BoYaOA { // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明, // 请访问 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add( new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" ); routes.MapRoute( "Default" , // 路由名称 "{controller}/{action}/{id}" , // 带有参数的 URL new { controller = "Home" , action = "Index" , id = UrlParameter.Optional } // 参数默认值 ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 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 { } } 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); } public void Application_Error() //Error是管道的事件 也可以从controller类看到 在前面加上Application_ 可以注册方法 放到mvc的GLOBAL { Exception ex = Server.GetLastError(); #region Logs var fileName = DateTime.Now.ToString( "yyyyMMdd" ) + ".txt" ; var actualLogPath = Server.MapPath( "/LogFiles/" + fileName); StringBuilder builderLogs = new StringBuilder(); builderLogs.AppendLine( "\r\n" ); builderLogs.AppendLine( "------------------------------------" + DateTime.Now.ToString()); builderLogs.Append(ex.ToString()); builderLogs.AppendLine( "\r\n" ); using (StreamWriter write = new StreamWriter(actualLogPath, true )) { write.Write(builderLogs.ToString()); } #endregion //Server.Transfer("/WebError.htm", true); //return "error"; } } } |
知识没有高低贵贱之分。
分类:
Asp.Net MVC
, javascript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?