The jquery and flash-based Multifile upload plug-in uploadify is indeed very useful. For detailed configuration and usage, see the previous article: A jquery-based file upload plug-in (.. Net version). But today I encountered a very headache when using this plug-in. When uploading files, my background sessions were suddenly lost, I went to debug and checked the session variable and found it to be null. Tragedy, can't I use this plug-in? Of course, this is impossible. Of course, such a good thing will be used, so we will find a solution.
Finally, the answer is correct (Non-IE browser), Because, for example, uploadify and swfupload use flash clients, the useragent generated by them and the user's browser
User-Agent must be different. Therefore, even if the user logs on to your system and generates a session, another session will be generated when the upload program is triggered (
When the useragent option is enabled ). Therefore, CI creates another session for uploadify when you upload a file instead of losing the session. Now that we can find the root cause of the problem, we can try to let the server manually pass the session value before the session is null.
The solution in ASP. NET is as follows:
Add the following code to the uploaded page:
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";var ASPSESSID = "<%= Session.SessionID %>";
Then the code of the initialization plug-in is changed to the following form:
$ ("# Fileinput1 "). uploadify ({'upload': '/scripts/Uploader/uploadify.swf', 'method': 'get', 'script': '/mystudio/gouploadavatar', 'cancelimg ': '/scripts/Uploader/cancel.png', 'sizelimmit ': 2048000, 'Multi': false, 'filedesc': 'select JPG, PNG, GIF', 'fileext ': '*. JPG ;*. PNG ;*. GIF ', 'oncomplete': function (E, queueid, fileobj, response, data) {}, 'onselectonce': function (E, data) {$ ('# fileinput1 '). uploadifysettings ('scriptdata', {'aspsessid': aspsessid, 'authid': AUTH });}});
Note that the above sentence is critical.
$('#fileInput1').uploadifySettings('scriptData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth });
Next, we must forcibly assign the passed sessonid to the cookies of the current request before the server session is empty and created, so that the server will think that the original session is passed over. The following code can be added to the global. asax file:
protected void Application_BeginRequest(object sender, EventArgs e) { /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ 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); }
At this time, when you access the page of the file to be uploaded, you may report that "the session status has already been created ."
Because the response has been refreshed by the application and cannot be saved, you can change the session storage mode in the web. config file.
"Inproc" storage, we change it to StateServer mode, that is, add it under the system. Web Node
<sessionstate mode="StateServer" stateconnectionstring="tcpip=127.0.0.1:42424" timeout="30"></sessionstate>
OK, the problem is solved, although it seems troublesome to solve this problem (I don't know how to solve it on other websites, at least in. net), but such a good File Upload plug-in is worth doing. I hope to provide some help to my friends who have encountered the same problem. Of course, if you have a better solution, you can leave a message to tell me, thank you very much.