//动态加载 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":<%=session_userid %> }); //上传开始 $('#uploadify').uploadifyUpload(); }
<%=session_userid %>是取后台的一个变量,该变量在加载页面的时候获得了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"); } }
这样就可以解决该问题了。