解决文件上传插件Uploadify在火狐浏览器下,Session丢失的问题

时间:2023-03-08 16:35:13
解决文件上传插件Uploadify在火狐浏览器下,Session丢失的问题

因为在火狐浏览器下Flash发送的请求不会带有cookie,所以导致后台的session失效。

解决的方法就是手动传递SessionID到后台。

 $("#fileresultfiles").uploadify({
swf: '/Scripts/uploadify/uploadify.swf',
uploader: '/UploadFiles.ashx',
queueID: 'fileQueue',
buttonText: '附件上传',
auto: true,
debug: false,
removeCompleted: true,
multi: true,
displayData: 'speed',
uploadLimit: 20,
fileSizeLimit: '500MB',
formData: {
'ASPSESSID': '<%=Session.SessionID %>',
'primarykey': $("#<%=hid_resultStrId.ClientID %>").val(),
},
onQueueComplete: function (queueData) {
//队列上传完成后,ajax获取上传的所有列表
AjaxRequestFile();
},
onSelectError: function (file, errorCode, errorMsg) {
var errorMsg = "";
if (errorCode == "QUEUE_LIMIT_EXCEEDED") {
errorMsg = "文件数量过多!";
}
if (errorCode == "-110") {
errorMsg = "文件大小超出限制!";
}
$('#' + file.id).find('.data').html(' errorMsg');
},
onUploadSuccess: function (file, data, response) {
if (data != "OK") {
$("#fileresultfiles").uploadify('cancel', '*');
alert(data);
}
}
});

'ASPSESSID': '<%=Session.SessionID %>'这句就是把SessionID传递过去

然后在Global.asax文件中监听每一个请求,如果有SessionID传过来就重新设置cookie

        protected void Application_BeginRequest(object sender, EventArgs e)
{
//为了Uploadify在谷歌和火狐下不能上传的BUG
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
{
}
} 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);//重新设定请求中的cookie值,将服务器端的session值赋值给它
}
}