Use of Session in Uploadify Upload plug-in

Source: Internet
Author: User
Tags auth httpcontext session id sessions access database server memory

Front end requires incoming session_id

$ (' #file_upload). Uploadify ({

//Your normal options here

formData: {' <?php echo session_name ();? > ': ' <?php echo session_id ();? > '}

});

The service side needs to obtain session_id information

$session _name = Session_name ();
if (!isset ($_post[$session _name])) {
exit;
} else {
session_id ($_post[$session _name]);
Session_Start ();
}

Uploadify in. Session lost solution in net


The solutions in ASP.net are 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%>";


The code for the initialization plug-in is then changed to the following form

$ ("#fileInput1"). Uploadify ({             ' uploader ') :  '/scripts/uploader/uploadify.swf ',               ':  ' get ',              ' script ':   '/mystudio/gouploadavatar ',              ' Cancelimg ':  '/scripts/uploader/cancel.png ',               ' SizeLimit ':  2048000,              ' Multi ':  false,              ' Filedesc ':  ' Choose Jpg,png,gif ',              ' fileext ':  ' *.jpg; *.png;*.gif ',                ' OnComplete ':  Function  (e, queueid, fileobj, response, data)  {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP},              ' onselectonce ': function  (e, data)  {                 $ (' #fileInput1 ') uploadifysettings (' Scriptdata ', {  ')

Aspsessid ': aspsessid,  ' authid ':  auth };             }  });


Note that there is a key.

$ (' #fileInput1 '). Uploadifysettings (' Scriptdata ', {' Aspsessid ': aspsessid, ' Authid ': auth});


Next we must assign the Sessonid to the currently requested cookie before the server session is empty and created, so that the service side thinks it is still the original session pass over. We can add the following code 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]);                 }           &NBSP;&NBSP;&NBSP}             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);         }

When you visit the page that uploaded the file, you may report "session state has created a session ID, but because the response has been refreshed by the application can not save it" error, at this point, you will be able to change the Web.config file memory, the general default is to "InProc" stored, we change it to StateServer mode, that is, to join the system.web node

<sessionstate mode= "StateServer" stateconnectionstring= "tcpip=127.0.0.1:42424" timeout= "></" Sessionstate>


A solution to uploadify losing session problem under Firefox


First talk about my environment, backstage is using the jsp,uploadify version is 3.2

Configuration in JSP page:

<script type= "Text/javascript" >

$ (document). Ready (function () {

$ ("#fileupload"). Uploadify ({

' SwF ': '/uploadify/scripts/uploadify.swf ',

' uploader ': '/fileupload;jsessionid=${pagecontext.session.id} ',

' auto ': true,

' multi ': false,

' buttontext ': ' BROWSE ',

' filedesc ': ' Support format: jpg/gif/jpeg/png/bmp. ', 
   ' fileext ': ' *.jpg;*.gif;*.jpeg;*.png;*.bmp ',

' onuploadsuccess ': onuploadsuccess

}); 

});

</script>

The key is the red word that part, to note that the front of the jsessionid is a semicolon rather than a question mark, written as a question mark as a parameter passed, hoping to help everyone less detours


Resolution of session loss in IIS7

Problem Description:

The internal Web system built by Windows Server 2008 +iis +asp.net +sqlserver2008.
The user session is always lost, possibly because of the instability of IIS that will cause session loss frequently.

Use the Session=sqlsever, that is, to save the session to the database.

Workaround:

1, enter the following address (installsqlstate.sql file directory) at the command line
CD "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727"

2, run the following command to create the database ASPState
Osql-s erpdbserver-u sa-p ***-i installsqlstate.sql

3, in the IIS server configuration session connection, the specific connection string reference page generated server=erpdbserver1; User Id=sa; password=*****
(Note: * Enter the database login password)

Summary of solutions for session loss

Recently in doing ASP.net project, the test site is always not get the value of the session, search the Internet for a while, find some solutions, recorded here. Finally, the problem is solved by using the method stored in StateServer.

sessionstate timeout), the main reason for this is three kinds

One: Some kill virus software will go to scan your Web.config file, at that time session affirmation drops, this is Microsoft's saying.
Two: Inside the program has let session loses the code, and the server memory is insufficient to produce.
Third: The program has frames page and cross domain situation.

The first solution is to make the virus-killing software screen scan the web.config file (not to edit it when the program is running)
The second is to check whether the code has Session.Abandon () or not.
The third is to start the ASP.net State Service in the window service.

Today encountered ASP.net session lost, and then came back, the session is stored in the user information, and finally caused each refresh is different users or not logged in state. It's a problem that makes people bleed.
2 days did not get out, and finally rewrite the verification mechanism, are placed in the cookie, the session is not used.
Today, the problem is due to the maximum number of web processes within the performance of the set IIS6 application pool, because the session defaults to the Web process, the process is much more open, resulting in sessions that cannot be shared, and each refresh is handled by different processes. This results in refreshing the different users, provided they are logged on to this computer.
Workaround you can use StateServer or SQL Server mode to store the session.
Because Google did not search a similar issue, so here to say this let me headache for 2 days, and finally modified the N-page problem.

asp.net session lost online collection
asp.net default configuration, the cause of the session inexplicable loss and solutions
Session will be lost for no reason under normal operation. Because the program is constantly being manipulated, excluding the possibility of session timeout. In addition, the session timeout is set to 60 minutes and will not be timed out so quickly.

This time to csdn to search a post, found that a lot of people are discussing this issue, and then I Google again, found that Microsoft has similar content on the site.

Now I'm going to write out the reason and the solution.

Reason:
Because the ASP.net program is the default configuration, the session setting in the Web.config file is as follows:
<sessionstate mode= ' InProc ' stateconnectionstring= ' tcpip=127.0.0.1:42424 ' sqlconnectionstring= ' data source= 127.0.0.1; Trusted_connection=yes ' cookieless= ' true ' timeout= '/>

We will find that there is an attribute mode in the sessionstate tag, which can have 3 values: InProc, StateServer? SQL Server (case sensitive). By default, it is InProc, which means that the session is stored in the process (IIS5 is aspnet_ Wp.exe, and IIS6 is W3wp.exe), this process is unstable, and when certain events occur, the process is reset, causing the session loss stored in the process.

Under what circumstances will the process be reset? An article from Microsoft has told us:
1. memorylimit attribute of processmodel label in configuration file
2, Global.asax or Web.config files are changed
3, the Web program (DLL) in the Bin folder was modified
4, antivirus software scanned some. config file.
For more information please refer to prb:session variables are lost intermittently in asp.net applications

Solution:
The mode attribute in the sessionstate label mentioned earlier can have three values, except for InProc, which can also be stateserver, SQL Server. These two methods of saving the session are all out-of-process, so when the aspnet_wp.exe is reset, it does not affect the sessions.

Now, set the mode to StateServer. StateServer is a service on this computer that can see services in the system service named ASP.net State Service, which is not started by default. When we set mode to StateServer, please start the service manually.

In this way, we can use the local stateservice to store the session, unless the computer restarts or Stateservice collapsed, the session will not be lost (due to the session timeout is discarded is normal).

In addition, we can also save the session through the stateservice of other computers. The specific changes are as follows. Also in the sessionstate tab, there is a stateconnectionstring= ' tcpip=127.0.0.1:42424 ' attribute with an IP address, which defaults to native (127.0.0.1), You can change it to the computer IP that you know is running the Stateservice service, so that you can realize the ASP.net program on different computers.

If you have higher requirements and you need to have a session not lost during the service restart, consider setting mode to SQL Server, as well as modifying the sqlConnectionString attribute. For an operation to save a session using SQL Server, visit here.

When you use StateServer or SQL Server to store sessions, all objects that need to be saved to the session must be serialized in addition to the basic data type (the default data type, such as int, string, and so on). Just put the [Serializable] label in front of the class you want to serialize.
Such as:
[Serializable]
public class MyClass
{
......
}
Specific serialization-related knowledge please join here.

So far, the problem is solved.
A summary of the problem of ASP.net session loss
How the session works in asp:
The ASP's session is process dependent. ASP session state is stored in the process of IIS, that is, inetinfo.exe this program. So when the Inetinfo.exe process crashes, the information is lost. In addition, the restart or shutdown of the IIS service can result in loss of information.

The realization of ASP.net session
asp.net session is based on HttpModule technology, HttpModule can be processed before the request to the State control of the request, because the session itself is used to do state maintenance, so with HttpModule to do the session is more appropriate.

Reason 1:
The files in the bin directory are overwritten, ASP.net has a mechanism that, in order to ensure that the system works correctly after the DLL is recompiled, it will restart the Web site process, which can cause session loss, so if an Access database is in the bin directory or if other files are overwritten by the system, the session is lost

Reason 2:
Folder Options, if the Open folder window in a separate process is not turned on, once you create a new window, the system may think it is a new session conversation, but you cannot access the original sessions, so you need to turn on this option, which will cause session loss

Reason 3:
It seems that most of the session loss is caused by the client, so to start from the client to see if the cookie is open

Reason 4:
Session time setting is not a problem, will not cause loss due to timeout

Reason 5:
The number of cookies in IE (20 cookies per domain) may cause session loss

Reason 6:
Use the Web garden mode and use InProc mode as a way to save the session

Resolve Lost Experience
1. To determine if the cause of the 1 caused, you can refresh the page each time, tracking a file in the bin modified time
2. Do session reading and writing log, every read and write session to record down, and to record SessionID, session value, the page, the current function, function, the first several sessions, so that the reason for the loss of a lot of convenience
3. It is recommended that you use state server or SQL Server to save the session if it is allowed, so that it is not easily lost
4. Add code to record the creation time and end time of the session in the Global.asa, which can be recorded in the sessionend when the timeout is lost.
5. If you use client script in some code, such as JavaScript to maintain session state, try to debug the script, whether it is due to a script error that causes session loss

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.