Home > Web Front-end > JS Tutorial > How to solve the Http Error 302 error in the jQuery upload plug-in Uploadify_jquery

How to solve the Http Error 302 error in the jQuery upload plug-in Uploadify_jquery

WBOY
Release: 2016-05-16 15:24:48
Original
1944 people have browsed it

I introduced how to use the jquery uploadify upload plug-in before. I encountered the Http Error 302 error problem during use. Many people should have encountered it during use. I will record it here:
First of all, http 302 means that the request was redirected, which is easy to understand. If your uploadify processing upload script has session verification, this error will occur because flash does not include cookie information when executing the post request, and the server The session will get the SESSIONID based on the client's cookie. Naturally, the session cannot be obtained without submitting the cookie, and uploadify returns a 302 (request redirected) error.
Solution:

Transmit the value of session_id to the server:

<script>
$(document).ready(function() { 
   $('#file_upload').uploadify({ 
    'uploader' : 'uploadify/uploadify.swf', 
    'script'  : 'uploadify.php',
    'folder'  : 'uploads/file', 
    'formData': { 'session': '<&#63;php echo session_id();&#63;>'}, 
    'onComplete' : function(event, ID, fileObj, response, data) { 
     alert(response); 
    } 
   }); 
}); 
</script>
Copy after login

Then before server-side session verification:

if (isset($_POST['session'])){ 
  session_id($_POST['session']); 
  session_start();//注意此函数要在session_id之后 
} 
Copy after login

Of course, you can also directly pass the session id in the URL, so that the Http Error 302 error can be solved.

Extension of the problem: HTTPERROR 302 error also occurs in normal firefox and chrome under MVC using uploadify3.1 IE. What is the solution?

jquery uploadify can upload normally under IE. When implementing asynchronous upload, each file will submit a request to the server when uploading. Each request requires security verification, session and cookie verification. Yes, that's it. Since jquery uploadify uses flash to achieve uploading, every time a data flow request is sent to the background, IE will automatically bundle the local cookie storage and send it to the server. But firefox and chrome will not do this, they will think it is unsafe.

First you need to add the following content to global.asxa

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);
    }
Copy after login

The initialization page upload plug-in code is as follows

<script type="text/javascript">
    var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null &#63; string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
    var ASPSESSID = "@Session.SessionID";

    $(function () {
      $('#upload').uploadify({
        'formData': { 'folder': '/Upload', 'ASPSESSID': ASPSESSID, 'AUTHID': auth },
        'buttonText': '浏览',
        'buttonClass': 'browser',
        'fileSizeLimit' : '100KB',
        'fileTypeExts': '*.xls;*.xlsx',
        'removeCompleted': false,
        'swf': '@Url.Content("~/Scripts/Uploadify/uploadify.swf")',
        'uploader': '/Upload',
        'onUploadSuccess': function (file, data, response) {}
      });
    });
  </script>

Copy after login

The research on a problem can be divergent and multifaceted. We must learn to draw inferences from one example so that we can flexibly learn professional knowledge and master professional skills. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template