5

我在我的网站上有一个文件上传,它是使用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
            }
        }
   }
}

在 IEContext.Session["User"]中总是有值,但在 Mozilla 中它总是为空

4

5 回答 5

11

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

我已经为会话和身份验证 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 上注册上述模块:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="SwfUploadSupportModule" type="namespace.SwfUploadSupportModule, application name" />
  </modules>
</system.webServer>
于 2010-12-27T11:46:04.883 回答
1

Context.Session is null.. because connection to HttpHandler has another Context.Session
(debug and try: Context.Session.SessionId in where is the fileInput is different from Context.Session.SessionId in Upload.ashx)!

I suggest a workaround: pass a reference to the elements you need in the second session ( in my sample i pass the original SessionId using sessionId variable)

....
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")%>',
 ....

and use this items in .ashx file.

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

If you need to share complex elements use Context.Application instead of Context.Session, using original SessionID: Context.Application["SharedElement"+SessionID]

于 2011-10-07T14:15:29.037 回答
0

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

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

这些接口不需要任何额外的代码,而是作为框架的标记。

希望这会有所帮助。

乔纳森

于 2013-04-01T18:23:42.277 回答
0

这很可能是服务器设置失败或发送回客户端失败。

退回到较低级别 - 使用FiddlerWireshark等网络诊断工具来检查发送到/来自服务器的流量,并比较 IE 和 Firefox 之间的差异。

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

于 2010-12-27T11:22:25.773 回答
0

我创建了一个函数来检查会话是否已过期,然后将其作为参数传递到uploadify 的脚本数据和ashx 文件中,我检查该参数以查看会话是否存在。如果它返回会话已过期,则上传将不会进行地方。它对我有用。使用它没有发现任何问题。希望能解决我的问题

于 2010-12-28T09:30:48.710 回答