A5下载文章资讯

分类分类

解决jQuery上传插件Uploadify出现Http Error 302错误的方法

2015-12-19 09:21作者:fang

之前介绍过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>

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

展开全部

相关

说两句网友评论
    我要跟贴
    取消