解决jQuery上传插件Uploadify出现Http Error 302错误的方法
之前介绍过jqueryuploadify上传插件的使用方法,我在使用中遇到过HttpError302错误问题,应该会有很多人在使用中遇到过,在此记录下来:
首先http302是请求被重定向的意思,这就很容易理解了,如果你的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':'<?phpechosession_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中将sessionid传过去,这样HttpError302错误就可以得到解决。
问题扩展:MVC使用uploadify3.1IE下正常firefox、chrome也出现HTTPERROR302错误,有什么解决办法?
jqueryuploadify在ie下可以正常上传,在实现异步上传的时候,每一个文件在上传时都会提交给服务器一个请求。每个请求都需要安全验证,session和cookie的校验。是的,就是这样。由于jqueryuploadify是借助flash来实现上传的,每一次向后台发送数据流请求时,ie会自动把本地cookie存储捆绑在一起发送给服务器。但firefox、chrome不会这样做,他们会认为这样不安全。
首先需要对global.asxa添加如下内容
protectedvoidApplication_BeginRequest(objectsender,EventArgse) { /*weguessatthispointsessionisnotalreadyretrievedbyapplicationsowerecreatecookiewiththesessionid...*/ try { stringsession_param_name="ASPSESSID"; stringsession_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]); } elseif(HttpContext.Current.Request.QueryString[session_param_name]!=null) { UpdateCookie(session_cookie_name,HttpContext.Current.Request.QueryString[session_param_name]); } } catch { } try { stringauth_param_name="AUTHID"; stringauth_cookie_name=FormsAuthentication.FormsCookieName; if(HttpContext.Current.Request.Form[auth_param_name]!=null) { UpdateCookie(auth_cookie_name,HttpContext.Current.Request.Form[auth_param_name]); } elseif(HttpContext.Current.Request.QueryString[auth_param_name]!=null) { UpdateCookie(auth_cookie_name,HttpContext.Current.Request.QueryString[auth_param_name]); } } catch { } } privatevoidUpdateCookie(stringcookie_name,stringcookie_value) { HttpCookiecookie=HttpContext.Current.Request.Cookies.Get(cookie_name); if(null==cookie) { cookie=newHttpCookie(cookie_name); } cookie.Value=cookie_value; HttpContext.Current.Request.Cookies.Set(cookie); }
初始化页面上传插件代码如下
<scripttype="text/javascript"> varauth="@(Request.Cookies[FormsAuthentication.FormsCookieName]==null?string.Empty:Request.Cookies[FormsAuthentication.FormsCookieName].Value)"; varASPSESSID="@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>
更多精彩内容请参考专题《ajax上传技术汇总》,《javascript文件上传操作汇总》和《jQuery上传操作汇总》进行学习。
一个问题的研究可以是发散性的是多方面,我们要学会举一反三,这样才能灵活的学习专业知识,掌握专业技能,希望对大家的学习有所帮助。