Solution to the jquery Upload plugin uploadify HTTP Error 302 error _jquery

Source: Internet
Author: User
Tags auth httpcontext session id

Before I introduced the jquery uploadify upload plug-in usage, I encountered in the use of HTTP error 302 errors, there should be many people in the use of the encounter, this record down:
First of all, HTTP 302 is the meaning of the request being redirected, which is easy to understand if your uploadify processing upload script has session validation, this error occurs because Flash does not contain cookie information when it executes the POST request. and the server session will be based on the client's cookie to get SessionID. The session cannot be obtained without submitting cookies, and then the Uploadify returns a 302 (Request redirected) error.
Solution:

Upload the session_id value to the server:

<script>
$ (document). Ready (function () { 
   $ (' #file_upload '). Uploadify ({ 
    ' uploader ': ' uploadify/ Uploadify.swf ', 
    ' script '  : ' uploadify.php ',
    ' folder '  : ' Uploads/file ', 
    ' formData ': {' Session ': ' <?php echo session_id ();? > '}, 
    ' OnComplete ': function (event, ID, Fileobj, response, data) { 
     alert (response); 
    } 
   } 
); 
</script>

Then before server-side session validation:

if (Isset ($_post[' Session '))} { 
  session_id ($_post[' Session ')); 
  Session_Start ()//Note that this function is to be session_id after the 
 

Of course, you can also pass the session ID directly in the URL, so that HTTP error 302 errors can be resolved.

problem Extension: MVC using uploadify3.1 ie under normal Firefox, Chrome also appears httperror 302 error, what is the solution?

jquery uploadify in IE can be normal upload, in the implementation of asynchronous upload, each file in the upload will be submitted to the server a request. Each request requires security verification, session, and cookie validation. Yes, that's it. Since the jquery uploadify is to upload with the help of Flash, every time the data stream request is sent to the background, IE will automatically bundle the local cookie store together to send to the server. But Firefox and Chrome won't do it, and they'll think it's not safe.

First you need to add the following to Global.asxa

protected void Application_BeginRequest (object sender, EventArgs e) {/* We guess at ' this ' is ' not ' Already retrieved by application so we recreate cookies 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, Ht
        Tpcontext.current.request.form[session_param_name]); else if (Httpcontext.current.request.querystring[session_param_name]!= null) {Updatecookie (s)
        Ession_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); } cookies.
      Value = Cookie_value;
    HttpContext.Current.Request.Cookies.Set (cookie);
 }

Initialize the page upload plug-in code as follows

<script type= "Text/javascript" >
    var auth = @ (request.cookies[formsauthentication.formscookiename]==null ? String. Empty:request.cookies[formsauthentication.formscookiename]. Value) ";
    var aspsessid = "@Session. SessionID";

    $ (function () {
      $ (' #upload '). Uploadify ({
        ' formData ': {' folder ': '/upload ', ' aspsessid ': aspsessid, ' Authid '): Auth},
        ' ButtonText ': ' Browse ',
        ' buttonclass ': ' Browser ',
        ' filesizelimit ': ' 100KB ',
        ' filetypeexts ': ' *. Xls;*.xlsx ',
        ' removecompleted ': false,
        ' swf ': ' @Url. Content ("~/scripts/uploadify/uploadify.swf") ',
        ' uploader ': '/upload ', '
        onuploadsuccess ': function (file, data, response) {}
  }); </script>

The study of a problem can be divergent is many, we should learn to extrapolate, so that can be flexible learning expertise, master professional skills, hope to help everyone's study.

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.