<script type="text/javascript">

    var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";

    var ASPSESSID = "@Session.SessionID";

    $(function () {

        $("#file_upload").uploadify({

            swf: '/Scripts/uploadify/uploadify.swf',

            uploader: '/Project/File/FileUpload',

            formData: { ASPSESSID: ASPSESSID, AUTHID: auth },

            auto: true,

            …

     });

注意:上面灰底代码很重要!

将下面代码放入Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)

        {

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

        }

  

我的uploadify页面

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link href="../../Content/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="../../Content/uploadify/jquery.uploadify.js" type="text/javascript"></script>
<script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
<link href="../../css/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<style type="text/css">
*
{
font-family: "microsoft yahei" , "Times New Roman" , "宋体" , Times, serif;
}
</style>
<script type="text/javascript">
var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
var ASPSESSID = "@Session.SessionID";
$(function () {
$('#uploadify').uploadify({
// debug: false, //开启调试
// successTimeout: 99999, //超时时间
formData: { //附带值
'userid': '用户id',
ASPSESSID: ASPSESSID,
AUTHID: auth
},
// queueID: 'some_file_queue', //文件选择后的容器ID
uploader: '/User/UploadUserImg', // 服务器端处理地址
swf: '../../Content/uploadify/uploadify.swf', // 上传使用的 Flash
width: 130, // 按钮的宽度
height: 25, // 按钮的高度
buttonText: "请选择上传的文件", // 按钮上的文字
buttonCursor: 'hand', // 按钮的鼠标图标
fileObjName: 'Filedata', // 上传参数名称
// 两个配套使用
fileTypeExts: "*.jpg;*.png", // 扩展名
fileTypeDesc: "请选择 jpg png 文件", // 文件说明
auto: false, // 选择之后,自动开始上传
multi: false, // 是否支持同时上传多个文件
queueSizeLimit: 1, //设置上传队列中同时允许的上传文件数量 允许多文件上传的时候,同时上传文件的个数
uploadLimit: 1, //uploadLimit:设置允许上传的文件数量,默认为999。
// removeCompleted: false, //设置已完成上传的文件是否从队列中移除,默认为true
onQueueComplete: function (queueData) {//队列中全部文件上传完成时触发事件。
alert(queueData.uploadsSuccessful + '个文件上传完成,确认后刷新页面' + queueData.uploadsErrored);
window.parent.afterUpdateImg();
},
//返回一个错误,选择文件的时候触发
onSelectError: function (file, errorCode, errorMsg) {
alert("error");
switch (errorCode) {
case -100:
alert("上传的文件数量已经超出系统限制的" + $('#uploadify').uploadify('settings', 'queueSizeLimit') + "个文件!");
break;
case -110:
alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#uploadify').uploadify('settings', 'fileSizeLimit') + "大小!");
break;
case -120:
alert("文件 [" + file.name + "] 大小异常!");
break;
case -130:
alert("文件 [" + file.name + "] 类型不正确!");
break;
}
},
onFallback: function () {
alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。"); //检测FLASH失败调用
},
onUploadSuccess: function (file, data, response) { //上传到服务器,服务器返回相应信息到data里
} });
});
</script>
<title>UploadUserImg</title>
</head>
<body>
<div>
<span id="uploadify"></span>
</div>
<div>
<button onclick="javascript:$('#uploadify').uploadify('upload','*');">
上传图片</button>
<button onclick="javascript:$('#uploadify').uploadify('cancel', '*');">
取消上传</button>
</div>
</body>
</html>

我的global

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;
using System.IO;
using System.Web.Security; namespace BoYaOA
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
} public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
); } protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
} protected void Application_BeginRequest(object sender, EventArgs e)
{
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);
}
public void Application_Error() //Error是管道的事件 也可以从controller类看到 在前面加上Application_ 可以注册方法 放到mvc的GLOBAL
{
Exception ex = Server.GetLastError();
#region Logs
var fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
var actualLogPath = Server.MapPath("/LogFiles/" + fileName);
StringBuilder builderLogs = new StringBuilder();
builderLogs.AppendLine("\r\n");
builderLogs.AppendLine("------------------------------------" + DateTime.Now.ToString());
builderLogs.Append(ex.ToString());
builderLogs.AppendLine("\r\n");
using (StreamWriter write = new StreamWriter(actualLogPath, true))
{
write.Write(builderLogs.ToString());
}
#endregion
//Server.Transfer("/WebError.htm", true);
//return "error";
} }
}

  

  

JQuery文件上传插件uploadify在MVC中Session丢失的解决方案的更多相关文章

  1. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...

  2. jQuery文件上传插件Uploadify(转)

    一款基于flash的文件上传,有进度条和支持大文件上传,且可以多文件上传队列. 这款在flash的基础上增加了html5的支持,所以在移动端也可以使用. 由于官方提供的版本是flash免费,html5 ...

  3. 关于jquery文件上传插件 uploadify 3.1的使用

    要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...

  4. jQuery文件上传插件uploadify

    官方网站:http://www.uploadify.com/ 参考博客:jQuery Uploadify在ASP.NET MVC3中的使用 参考博客:使用uploadify上传图片时返回“Cannot ...

  5. 文件上传插件Uploadify在Struts2中的应用,完整详细实例

    —>最近由于项目需要使用到一个上传插件,在网上发现uploadify挺不错,所以决定使用它,但是官网文档和例子是php的,而项目是SSI框架的,所以自己对uploadify在struts2中的使 ...

  6. 关于jquery文件上传插件 uploadify 3.1的使用

    要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...

  7. jQuery 文件上传插件:uploadify、swfupload

    jQuery 文件上传插件: uploadify.swfupload

  8. 20款最好的jQuery文件上传插件

    当它是关于开发网络应用程序和网页的时候,文件上传功能的重要性是不容忽视的.一个文件上传功能可以让你上传所有类型的文件在网站上,包括视频,图像,文件和更多.创建一个文件上传功能,对你的网站是不是很难,有 ...

  9. jQuery文件上传插件jQuery Upload File 有上传进度条

    jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...

随机推荐

  1. python基础——字符串和编码

    python基础——字符串和编码 字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用 ...

  2. CentOS下配置Hadoop集群:java.net.NoRouteToHostException: No route to host问题的解决

    我用的是hadoop 1.2.1 遇到的问题是: hadoop中datanode无法启动,报Caused by: java.net.NoRouteToHostException: No route t ...

  3. gitlab web登入密码忘记以后可以用如下方式修改密码

    ➜ ~ gitlab-rails console production Loading production environment (Rails ) irb(main)::> ➜ ~ gitl ...

  4. kvm 克隆虚拟机

    两步: 第一步导出XML: [root@ok ~]# virsh dumpxml centos02 >12c.xml 第二步磁盘文件 [root@ok virhost]# cp centos02 ...

  5. 基于Twemproxy的Redis集群方案

    概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的缓存性能及扩展型,我们将需要搭建redis集群来满足需 ...

  6. ***微信公众平台开发: 获取用户基本信息+OAuth2.0网页授权

    本文介绍如何获得微信公众平台关注用户的基本信息,包括昵称.头像.性别.国家.省份.城市.语言.本文的方法将囊括订阅号和服务号以及自定义菜单各种场景,无论是否有高级接口权限,都有办法来获得用户基本信息, ...

  7. ajax请求原理及jquery $.ajax封装全解析

    .ajax原理: Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其中最关键的一步就是从服务器获得 ...

  8. poj 2392 多重背包

    题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...

  9. oracle创建临时表没有权限

    执行下面: grant create any table to 用户名称

  10. 编程第一个Apple Watch程序创建项目

    编程第一个Apple Watch程序创建项目 2.4  编程第一个程序 本节将通过编写第一个程序,为开发者讲解如何添加Watch应用对象.运行程序.界面设计.编写代码等内容本文选自Apple Watc ...