发布于 2017-02-20 23:18:51 | 201 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的jQuery示例,程序狗速度看过来!

jQuery javascript框架

jQuery是一个兼容多浏览器的javascript框架,核心理念是write less,do more(写得更少,做得更多)。jQuery在2006年1月由美国人John Resig在纽约的barcamp发布,吸引了来自世界各地的众多JavaScript高手加入,由Dave Methvin率领团队进行开发。


这篇文章主要介绍了解决jQuery上传插件Uploadify出现Http Error 302错误的方法

之前介绍过jquery uploadify上传插件的使用方法,我在使用中遇到过Http Error 302错误问题,应该会有很多人在使用中遇到过,在此记录下来:
首先http 302是请求被重定向的意思,这就很容易理解了,如果你的uploadify处理上传脚本有session验证,就会出现此错误,因为flash在执行post请求的时候没有包含cookie信息,而服务器的session会根据客户端的cookie来得到SESSIONID。没有提交cookie自然就不能获取到session,然后uploadify就返回了302(请求被重定向)的错误。
解决办法:

把session_id的值传到服务端:


<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>

然后在服务器端session验证之前:


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

当然,你也可以直接在url中将session id传过去,这样Http Error 302错误就可以得到解决。

问题扩展:MVC使用uploadify3.1 IE下正常firefox、chrome也出现HTTPERROR 302错误,有什么解决办法?

jquery uploadify在ie下可以正常上传,在实现异步上传的时候,每一个文件在上传时都会提交给服务器一个请求。每个请求都需要安全验证,session 和cookie的校验。是的,就是这样。由于jquery uploadify是借助flash来实现上传的,每一次向后台发送数据流请求时,ie会自动把本地cookie存储捆绑在一起发送给服务器。但 firefox、chrome不会这样做,他们会认为这样不安全。

首先需要对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);
    }

初始化页面上传插件代码如下


<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': '浏览',
        'buttonClass': 'browser',
        'fileSizeLimit' : '100KB',
        'fileTypeExts': '*.xls;*.xlsx',
        'removeCompleted': false,
        'swf': '@Url.Content("~/Scripts/Uploadify/uploadify.swf")',
        'uploader': '/Upload',
        'onUploadSuccess': function (file, data, response) {}
      });
    });
  </script>

 一个问题的研究可以是发散性的是多方面,我们要学会举一反三,这样才能灵活的学习专业知识,掌握专业技能,希望对大家的学习有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务