uploadify session java_解决uploadify使用时session发生丢失问题的方法
今天在使用uploadify时发现session会发生丢失的情况,经过一番研究发现,其丢失并不是真正的丢失,而是在使用Flash上传控件的时候使用的session机制和asp.net中的不相同。为解决这个问题使用两种方案,下面进行介绍
第一种:修改Gobal前台aspx页面:
$("#uploadify").uploadify({
'uploader': '/LZKS/Handler/BigFileUpLoadHandler.ashx',
'swf': '/LZKS/Scripts/uploadify/uploadify.swf',
'cancelImage': '/LZKS/Scripts/uploadify/cancel.png',
'queueID': 'fileQueue',
//'auto': false,
'multi': true,
'buttonText': '文件上传',
'formData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth },
'onSelect': function (file) {
$('#uploadify').uploadifySettings('formData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth });
alert(formDate);
},
'onComplete': function (file, data, response) {
},
'onQueueComplete': function () {
alert("上传完成!");
$('#fileQueue').attr('style', 'visibility :hidden');
},
'onSelectError': function (file, errorCode, errorMsg) {
$('#fileQueue').attr('style', 'visibility :hidden');
},
'onUploadStart': function (file) {
$('#fileQueue').attr('style', 'top:200px;left:400px;width:400px;height :400px;visibility :visible');
}
});
});
接着修改Gobal中的代码:
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);
}
在JS加载前面定义下面两个变量
var auth = "";
var ASPSESSID = "";
Handler文件代码如下:
public class BigFileUpLoadHandler : IHttpHandler, IRequiresSessionState
{
DALFile Fdal = new DALFile();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
VideoUpLoad(context, CLSOFT.Web.LZKS.Edu.Globe.filename);
}
public void VideoUpLoad(HttpContext context, string fileFolderName)
{
context.Response.Charset = "utf-8";
string aaaaaaa=context.Request.QueryString["sessionid"];
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath(UploadFileCommon.CreateDir(fileFolderName));
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
Model.ModelFile model = new Model.ModelFile();
model.File_ID = Guid.NewGuid().ToString();
model.File_Name = file.FileName;
model.File_Path = UploadFileCommon.CreateDir(fileFolderName);
model.File_Size = file.ContentLength;
model.File_Extension = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1);
model.File_Date = DateTime.Now;
model.File_CurrentMan = CLSOFT.Web.LZKS.Edu.Globe.name;
file.SaveAs(uploadPath + model.File_Name);
List list = null;
if (context.Session["File"] == null)
{
list = new List();
}
else
{
list = context.Session["File"] as List;
}
list.Add(model);
context.Session.Add("File", list);
}
else
{
context.Response.Write("0");
}
}
这段代码的功能是将多文件的信息存到context.Session["File"] as List为文件信息类 实现批量上传的信息给Session
第二种方案:直接向后台传递session值
Ext.onReady(function () {
Ext.QuickTips.init();
$("#uploadify").uploadify({
'uploader': '../Uploadify-v2.1.4/uploadify.swf',//上传swf相对路径
'script': '../Service/FileUploadHelper.ashx',//后台上传处理呈现
'cancelImg': '../Uploadify-v2.1.4/cancel.png',//取消上传按钮相对路径
'checkExisting':true,//服务端重复文件检测
'folder': '../UploadFile/',//上传目录
'fileExt':'*.jpg;*.png;*.gif;*.bmp',//允许上传的文件格式
'fileDesc':'jpg、png、gif、bmp',//文件选择时显示的提示
'queueID': 'fileQueue',//上传容器
'auto': false,
'multi': false,//只允许单文件上传
'buttonText':'Choose File',
'scriptData': { 'name': '', 'type': '','length':'' },//在加载时此处是null
//'onInit':function(){alert("1");},//初始化工作,在Extjs的嵌套中最先触发的函数
//选择一个文件后触发
'onSelect': function(event, queueID, fileObj) {
// alert("唯一标识:" + queueID + "\r\n" +
// "文件名:" + fileObj.name + "\r\n" +
// "文件大小:" + fileObj.size + "\r\n" +
// "创建时间:" + fileObj.creationDate + "\r\n" +
// "最后修改时间:" + fileObj.modificationDate + "\r\n" +
// "文件类型:" + fileObj.type);
$("#uploadify").uploadifySettings("scriptData", { "length": fileObj.size}); //动态更新配(执行此处时可获得值)
},
//上传单个文件接收后触发
'onComplete': function (event, queueID, fileObj, response, data) {
var value = response;
if(value==1){
Ext.Msg.alert("提示","上传成功");
}
else if(value==0){
Ext.Msg.alert("提示","请选择上传文件");
}
else if(value==-1){
Ext.Msg.alert("提示","已存在该文件");
}
}
});
动态的传递参数,并判断是否合法
//动态加载
function loadFileType(){
//检测
var medianame=Ext.getCmp("eName").getValue();
if(medianame.trim()==""){
Ext.Msg.alert("提示","媒体名称不能为空");
return;
}
var filetype=Ext.getCmp("eType").getValue();
if(filetype=="" || filetype<0){
Ext.Msg.alert("提示","请选择媒体类型");
return;
}
//动态更新配(执行此处时可获得值)
$("#uploadify").uploadifySettings("scriptData", { "name": medianame,"type":filetype,"sessionuserid": });
//上传开始
$('#uploadify').uploadifyUpload();
}
是取后台的一个变量,该变量在加载页面的时候获得了session值。当然也可以在前台直接获得session值。
后台处理程序:
public class FileUploadHelper : IRequiresSessionState, IHttpHandler
{
int nCurrentUserID = -1;
public void ProcessRequest(HttpContext context)
{
try
{
nCurrentUserID = WebUtil.GetCurrentUserID();//该处的session值得不到
}
catch (Exception)
{
}
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
string strFilename = string.Empty;
int nFiletype = 0;
float fFilelength = 0;
string strFileExt = string.Empty;
string strFilePath = string.Empty;
if (context.Request["sessionuserid"] != null)
{
nCurrentUserID = Convert.ToInt32(context.Request["sessionuserid"].ToString());
}
if (context.Request["name"] != null)//获得文件名(动态参数)
{
strFilename = context.Request["name"].ToString();
}
if (context.Request["type"] != null)//获得文件类型(动态参数)
{
nFiletype = Convert.ToInt32(context.Request["type"].ToString());
}
if (context.Request["length"] != null)//获得文件长度(动态参数)
{
int nEmptFileLength = Convert.ToInt32(context.Request["length"].ToString());
fFilelength = (float)nEmptFileLength / 1024;
}
if (context.Request["Filename"] != null)//获得文件名(系统自带)
{
string filename = context.Request["Filename"].ToString();
strFileExt = Path.GetExtension(filename).ToLower();//获得后缀名
}
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]);
//根据当前日期创建一个文件夹
string dirName = System.DateTime.Now.ToString("yyyyMMdd");
uploadPath += dirName;
string tmpRootDir = context.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
if (file != null)
{
//判断目录是否存在
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
//判断文件是否存在
strFilePath = uploadPath + "\\" + strFilename + strFileExt;
if (!File.Exists(strFilePath))
{
//写数据库成功保存文件
Media model = new Media();
int newMediaID = -1;
model.media_type = nFiletype;
model.media_name = strFilename + strFileExt;
model.file_path = strFilePath.Replace(tmpRootDir, "");//保存相对目录
model.file_length = fFilelength;
newMediaID = MediaBLL.AddMadia(model, nCurrentUserID);
if (newMediaID > -1)//数据库写入成功
{
//保存文件
file.SaveAs(strFilePath);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
context.Response.Write("1");
}
}
else
{
context.Response.Write("-1");
}
}
else
{
context.Response.Write("0");
}
}
这样就可以解决该问题了。
希望这两种方法都能帮助大家顺利解决session丢失问题,谢谢大家的阅读。
uploadify session java_解决uploadify使用时session发生丢失问题的方法相关推荐
- python编译环境对cpu要求高不高_解决Tensorflow 使用时cpu编译不支持警告的问题
使用TensorFlow模块时,弹出错误Your CPU supports instructions that this TensorFlow binary was not compiled to u ...
- 解决rimraf使用时提示unexpected token “.”
解决rimraf使用时提示unexpected token "." 前言 最近运行一个Cordova项目时,npm install后打包,命令栏提示了下面这个问题: 很奇怪啊,就我 ...
- npm全局环境变量配置及解决VsCode使用时遇到的问题
一.npm全局环境变量配置 1.我们要先配置npm的全局模块的存放路径以及cache的路径 例如我希望将以上两个文件夹放在NodeJS的主目录下,便在NodeJs下建立"node_globa ...
- 解决Tensorflow 使用时cpu编译不支持警告
运行代码之后,控制台除了输出应该有的结果外,还多了一行: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.c ...
- 多媒体一体机计算机打不开,多媒体教学会议一体机使用时遇到的问题及使用方法...
1.使用多媒体教学会议一体机的过程中可能出现的问题及解决办法 计算机在使用过程中,出现触摸屏定位不准现象. 解决办法:用面纸擦除一体机屏幕四周边框内侧的灰尘,特别是下方的粉笔灰. 计算机在使用过程中, ...
- 电线电缆使用时的安全要求和存放方法
电线电缆产品种类众多,应用范围十分广泛,如果存放不当会影响线缆的寿命及使用安全,甚至造成不必要的灾害,所以如何存放就是一个需要重视的问题. 一.安全要求 1.电缆线相互交叉时,高压电缆应在低压电缆下方 ...
- Java中解决(extjs或jquery)session过期退出登录问题
为什么80%的码农都做不了架构师?>>> 解决两种情况下的用户访问超时: a)普通http请求的session超时: b)异步http请求的session超时,如果使用extj ...
- AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘错误解决
AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 根据网上教程安装tensorflow环境时,最后都会有个测试代码用 ...
- Shiro结合Redis解决集群中session同步问题
Shiro结合Redis解决集群中session同步问题 参考文章: (1)Shiro结合Redis解决集群中session同步问题 (2)https://www.cnblogs.com/Luke-M ...
最新文章
- Vue 组件库 heyui@1.18.0 发布,新增地址选择、图片预览组件
- gz解压java,java 解压gz
- RxSwift 小记 Error Handling Operators(catchError,retry)
- 概率编程库Pymc3案例之神经网络(批量训练)
- 通过阿里云容器镜像服务海外服务器构建spark-operator镜像
- python打包工具哪个好用_python打包工具比较
- centos 7.6安装java_安装 QRadar Community Edition
- 查看oracle死锁进程并结束死锁
- web开发为什么用java的多_java与php做web开发 最大的区别在那 为什么好多用java的...
- 微会显示服务器当前线路忙,免费电话之争:触宝电话/微会谁更强
- Fixchart图表组件——仪表盘,纳尼?
- 二十一天学通C++之使用throw抛出异常
- 【转】HTTP请求中的form data和request payload的区别
- 数据持久化,序列化,反序列化,文件读写1
- 模板方法模式-Template Method Pattern
- Visual Studio 2019 C++实现socket通信,添加ws2_32.lib库,新手代码
- 串口屏还是并口屏好用?
- 学习机器视觉需要掌握哪些知识?【转】
- 坑人的青旅乐山峨眉两日游
- 【PS基础教程】PS制作圆形小图标