Uploadify ashx文件Context.Session获取null

我在我的网站上有一个文件上传,这是使用uploadify完成的,它使用ashx页面将文件上传到数据库。它在IE中工作正常,但在Mozilla中,context.Session变为null。我也使用IReadOnlySessionState来读取会话。

我怎么能像IE一样在Mozilla中获得会话。

这是我做过的ashx代码

 public class Upload : IHttpHandler, IReadOnlySessionState { HttpContext context; public void ProcessRequest(HttpContext context) { string UserID = context.Request["UserID"]; context.Response.ContentType = "text/plain"; context.Response.Expires = -1; XmlDocument xDoc = new XmlDocument(); HttpPostedFile postedFile = context.Request.Files["Filedata"]; try { if (context.Session["User"] == null || context.Session["User"].ToString() == "") { context.Response.Write("SessionExpired"); context.Response.StatusCode = 200; } else { // does the uploading to database } } } } 

在IE中, Context.Session["User"]始终具有值,但在Mozilla中它始终为null

您需要添加sessionId以上传post params并在OnBeginRequest上的global.asax上恢复服务器端的ASP.NET_SessionId cookie。 它实际上是闪存和cookie的错误 。

我已经为会话和auth cookie恢复创建了模块,以获得工作flash和asp.net会话,所以我认为它对你有用:

 public class SwfUploadSupportModule : IHttpModule { public void Dispose() { // clean-up code here. } public void Init(HttpApplication application) { application.BeginRequest += new EventHandler(OnBeginRequest); } private void OnBeginRequest(object sender, EventArgs e) { var httpApplication = (HttpApplication)sender; /* 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 (httpApplication.Request.Form[session_param_name] != null) { UpdateCookie(httpApplication, session_cookie_name, httpApplication.Request.Form[session_param_name]); } else if (httpApplication.Request.QueryString[session_param_name] != null) { UpdateCookie(httpApplication, session_cookie_name, httpApplication.Request.QueryString[session_param_name]); } } catch { } try { string auth_param_name = "AUTHID"; string auth_cookie_name = FormsAuthentication.FormsCookieName; if (httpApplication.Request.Form[auth_param_name] != null) { UpdateCookie(httpApplication, auth_cookie_name, httpApplication.Request.Form[auth_param_name]); } else if (httpApplication.Request.QueryString[auth_param_name] != null) { UpdateCookie(httpApplication, auth_cookie_name, httpApplication.Request.QueryString[auth_param_name]); } } catch { } } private void UpdateCookie(HttpApplication application, string cookie_name, string cookie_value) { var httpApplication = (HttpApplication)application; HttpCookie cookie = httpApplication.Request.Cookies.Get(cookie_name); if (null == cookie) { cookie = new HttpCookie(cookie_name); } cookie.Value = cookie_value; httpApplication.Request.Cookies.Set(cookie); } } 

还需要在web.config上注册上面的模块:

      

Context.Session is null .. 因为与HttpHandler连接有另一个Context.Session
(调试并尝试: Context.Session.SessionId在哪里,fileInput与Upload.ashx中的Context.Session.SessionId不同)!

我建议一个解决方法: 在第二个会话中传递对所需元素的引用 (在我的示例中,我使用sessionId变量传递原始的SessionId

 .... var sessionId = "<%=Context.Session.SessionID%>"; var theString = "other param,if needed"; $(document).ready(function () { $('#fileInput').uploadify({ 'uploader': '<%=ResolveUrl("~/uploadify/uploadify.swf")%>', 'script': '<%=ResolveUrl("~/Upload.ashx")%>', 'scriptData': { 'sessionId': sessionId, 'foo': theString }, 'cancelImg': '<%=ResolveUrl("~/uploadify/cancel.png")%>', .... 

并在.ashx文件中使用此项目。

 public void ProcessRequest(HttpContext context) { try { HttpPostedFile file = context.Request.Files["Filedata"]; string sessionId = context.Request["sessionId"].ToString(); .... 

如果需要共享复杂元素,请使用Context.Application而不是Context.Session ,使用原始SessionID: Context.Application["SharedElement"+SessionID]

它很可能是服务器无法设置或在客户端上发回的内容。

退回到较低级别 – 使用Fiddler或Wireshark等网络诊断工具检查发送到服务器或从服务器发送的流量,并比较IE和Firefox之间的差异。

查看标头以确保将cookie和表单值按预期发送回服务器。

我创建了一个检查会话已过期的函数,然后将其作为参数传递给uploadify的脚本数据,并在ashx文件中检查该参数以查看会话是否存在。如果它返回会话已经过期,那么上传将不会place.It为我工作。 没有发现使用它的任何问题。 希望能解决我的问题

我的.ashx文件有类似的问题。 解决方案是处理程序必须实现IReadOnlySessionState(用于只读访问)或IRequiresSessionState(用于读写访问)。 例如:

 public class SwfUploadSupportModule : IHttpHandler, IRequiresSessionState { ... } 

这些接口不需要任何其他代码,但可以充当框架的标记。

希望这会有所帮助。

乔纳森