(Switch) the problem that flash cannot transmit session and cookie, which is thought of by uploadify plug-in, is solved.

Source: Internet
Author: User
Tags php server

In ASP. when using uploadify in net mvc3 to upload files, it is found that uploadify cannot complete authentication when the background needs to verify the logon status. Therefore, you can only upload files in non-Authentication status in the background-as we all know, this is a great opportunity for non-logged-on upset people.

To solve this problem, we need to understand uploadify, at least when I start to solve this problem. this special section is found in the help document of uploadify's official website:Using sessions with uploadify.

This article is undoubtedly a good start for us to solve the problem. Maybe you will ask, does the client also have a session? If you consider it from the server perspective, I can tell you with certainty, of course -- no! An HTTP request is a stateless request. The existence of a session continues with the exchange mark between the client and the server. In most cases, sessionid is set in cookies. When the client does not support cookies, a flag may be added to the URI to achieve the same purpose. Further discussions are beyond the scope of this article. Here, we want to solve the problem more specifically and narrow down the problem to how uploadify transfers cookies to the server.

In the document mentioned above, the official PHP Solution is provided, and the code is very simple:

/* PHP initializes uploadify code on the client */

$ ('# File_upload). uploadify ({
// Your normal options here
Formdata: {'<? PHP echo session_name ();?> ':' <? PHP echo session_id ();?> '}
});

 

/* PHP server code */

$ Session_name = session_name ();

If (! Isset ($ _ post [$ session_name]) {
Exit;
} Else {
Session_id ($ _ post [$ session_name]); // set the current sessionid to the sessionid passed back by the client.
Session_start ();
}

From the code, we can see that the specific ideas for official processing are as follows: 1. when loading pages containing uploadify, write the sessionid in cookies to formdata in uploadify; 2. when submitted by the client, sessionid will be submitted to the server as form data by uploadify; 3. in the server detection, extract the submitted sessionid and use the submitted sessionid as the blueprint for the current status. In this way, the operation can be verified to achieve the purpose of control.

With this blueprint, it is not difficult to solve it in ASP. NET mvc3. Just follow the official idea:

1. First, save the current status to formdata:

/* View code, use razor syntax .*/

$ ("# Fileupload"). uploadify ({
'Formdata ':{
@ Foreach (string K in request. Cookies. allkeys ){
@: '[Email protected] (k)': '@ request. Cookies [K]. value ',
}
'':''}

})

In order to fully simulate the client status, we put all cookies in formdata, and, in order to distinguish, we use the key value names to start with Cookie.

2. after the client is secure, we need to process the server. In MVC, we do not have the PHP session_id (ID) method available (if you have one, please let me know ~), We need to set it in global. asax. The Code is as follows:

Protected void application_beginrequest (Object sender, eventargs E)
{
Try
{
Httprequest request = httpcontext. Current. request;
Foreach (string K in request. Form. Keys)
{
If (K. indexof ("Cookie _")> = 0)
Appendingcookies (K. Remove (0, 7), request. Form [K], request );
}
}
Catch {}
}

Private void appendingcookies (string cookiename, string cookievalue, httprequest request)
{
Httpcookie Hc = request. Cookies. Get (cookiename );
If (null = HC)
Hc = new httpcookie (cookiename );
HC. value = cookievalue;
Request. Cookies. Set (HC );
}

When starting a request, we intercept the request and check the form data submitted in the request. If any data starting with "Cookie _" is found, we extract the data, set to cookies of the current request.

Well, it looks perfect now. uploadify is no different from a normal request. However, there is a small defect, that is, every request, regardless of the data requested, will perform such operations. If other forms really contain data starting with "Cookie, this is really not good. To solve this problem, we need to make some improvements to the operation. The improved code is as follows:

Protected void application_beginrequest (Object sender, eventargs E)
{
Try
{
Routedata RD = routetable. routes. getroutedata (httpcontextbase) New httpcontextwrapper (httpcontext. Current ));
String c = RD. getrequiredstring ("controller ");
String A = RD. getrequiredstring ("action ");
If (C. tolower () = "uploadfile" & A. tolower () = "Upload ")
{
Httprequest request = httpcontext. Current. request;
Foreach (string K in request. Form. Keys)
{
If (K. indexof ("Cookie _")> = 0)
Appendingcookies (K. Remove (0, 7), request. Form [K], request );
}
}
}
Catch {}
}

Private void appendingcookies (string cookiename, string cookievalue, httprequest request)
{
Httpcookie Hc = request. Cookies. Get (cookiename );
If (null = HC)
Hc = new httpcookie (cookiename );
HC. value = cookievalue;
Request. Cookies. Set (HC );
}

Here, uploadfile is the Controller in MVC and upload is the action. before the operation, we can check whether the current request is a file upload. If yes, we will execute these operations, the possibility of a problem is reduced.

Here, we have completely solved the uploadify status problem, and even better than what the official team handles. In this case, let's get to the end first. As we mentioned at the beginning, this is not a problem exclusive to uploadify, but a flash issue. Therefore, what should we do when using flash to interact with the system? In fact, when solving the problem of File Upload verification, we have learned how to solve this problem. We can complete the operation by referring to the uploadify processing method:

1. Put the current request status of the server into the code for client segment preparation and flash interaction (JS or HTML );

2. The client uses flash to encapsulate the Request status that has been stored in the client into the flash request and submit it to the server;

3. The server extracts the cookies from the flash request and sets them to the cookies of the current request.

Another method is to export the data in flash and use Js for submission. In this way, you only need to extract the interactive data in flash, JS requests will automatically pass cookies from the client to the server. This method is relatively simple and efficient. Uploadify also provides the HTML5 version, but it only charges fees, but I believe this method should be used.

 

This article is original and reprinted with the source: mitchellchu's blog

 

 

 

$( function (){      setTimeout( function (){          $( ‘#file_upload‘ ).uploadify({              ‘swf‘ : ‘tools/uploadify/uploadify.swf‘ ,              ‘uploader‘ : ‘upload.php‘ ,              ‘onUploadSuccess‘ : function (file, data, response) {                               }          });      },10); });

(Switch) the problem that flash cannot transmit session and cookie, which is thought of by uploadify plug-in, is solved.

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.