簡體   English   中英

Uploadify ashx文件Context.Session獲取null

[英]Uploadify ashx file Context.Session gets 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上注冊以上模塊:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="SwfUploadSupportModule" type="namespace.SwfUploadSupportModule, application name" />
  </modules>
</system.webServer>

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]

我的.ashx文件有類似的問題。 解決方案是處理程序必須實現IReadOnlySessionState(用於只讀訪問)或IRequiresSessionState(用於讀寫訪問)。 例如:

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

這些接口不需要任何其他代碼,但可以充當框架的標記。

希望這會有所幫助。

喬納森

它很可能是服務器未能設置或在客戶端上發回的內容。

退回到較低級別 - 使用FiddlerWireshark等網絡診斷工具檢查發送到服務器或從服務器發送的流量,並比較IE和Firefox之間的差異。

查看標頭以確保將cookie和表單值按預期發送回服務器。

我創建了一個檢查會話已過期的函數,然后將其作為參數傳遞給uploadify的腳本數據,並在ashx文件中檢查該參數以查看是否存在會話。如果它返回會話已經過期,則上傳將不會place.It為我工作。 沒有發現使用它的任何問題。 希望能解決我的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM