解决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的值传到服务端:

rush:js;">

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

rush:js;"> 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添加如下内容

rush:xhtml;"> 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_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name] != null)
{
 UpdateCookie(session_cookie_name,HttpContext.Current.Request.Form[session_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name]);
}
else if (HttpContext.Current.Request.QueryString[session_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name] != null)
{
 UpdateCookie(session_cookie_name,HttpContext.Current.Request.QueryString[session_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name]);
}

}
catch
{
}

try
{
string auth_param_name = "AUTHID";
string auth_cookie_name = FormsAuthentication.FormsCookieName;

if (HttpContext.Current.Request.Form[auth_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name] != null)
{
 UpdateCookie(auth_cookie_name,HttpContext.Current.Request.Form[auth_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name]);
}
else if (HttpContext.Current.Request.QueryString[auth_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_name] != null)
{
 UpdateCookie(auth_cookie_name,HttpContext.Current.Request.QueryString[auth_p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m_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);
}

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

rush:js;">

更多精彩内容请参考专题上传操作汇总》进行学习。

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

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...