locked
Accessing Session Variables In HttpHandler RRS feed

  • Question

  • User1780379888 posted

    Hi

    Based on Mudassar Khan's article "Select and Upload Multiple Files Gmail Style using JQuery and ASP.Net", I am trying to modify the HttpHandler to be able to access Session variables set in the AutoUpload.aspx page

    I am trying to create unique folders for the file uploaded by accessing a session variable set in the codebehind page of AutoUpload.aspx. Before the file is uploaded, the folder name which is the session variable is created. But the session variable returns null in the httpHandler page. I have inherited the interface IReadOnlySessionState and included the namespace System.Web.SessionState in the HttpHandler page. Is there anything else I have missed. Any suggestion or advise is appreciated. Thanks

    Following is the code I am using

    Upload.ashx

    <%@ WebHandler Language="C#" Class="Upload" %>
    
    using System;
    using System.Web;
    using System.IO;
    using System.Web.SessionState;
    
    public class Upload : IHttpHandler,IReadOnlySessionState {
        
        public void ProcessRequest (HttpContext context) {
            
            string fid = Convert.ToString(context.Session["fid"]);
    
            string folderId = String.Empty;
    
            if (String.IsNullOrEmpty(fid))
            {
                    folderId = "uploads/_test";
            }
            else
            {
                    folderId = "uploads/" + fid;
            }
            try
            {
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
                
                string savepath = "";
                string tempPath = "";
                tempPath = folderId; 
                savepath = context.Server.MapPath(tempPath);
                string filename = postedFile.FileName;
    
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);
    
                postedFile.SaveAs(savepath + @"\" + filename);
                context.Response.Write(tempPath + "/" + filename);
                context.Response.StatusCode = 200;
            }
            catch (Exception ex)
            {
                context.Response.Write("Error: " + ex.Message);
            }
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    }



    AutoUpload.aspx.cs

    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {           
            if (!Page.IsPostBack)
            {
                    Session["fid"] = System.Guid.NewGuid().ToString();
                     ...
            }
        }
    }



    Since the session variable always returns null, the file is uploaded to the _test folder and the unique folder is never created.



    Thursday, September 15, 2011 11:42 PM

Answers

  • User1613327603 posted

    public string strTemp
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["strTemp"] != null)
                {
                    return (System.Web.HttpContext.Current.Session["strTemp"].ToString());
                }
                return null;
            }
            set
            {
                System.Web.HttpContext.Current.Session["strTemp"] = value;
            }
        }

    please create above property in common/global/statis class and access this property for store and retrive value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 17, 2011 7:21 AM

All replies

  • User-168800120 posted

    Instead of session use query string and access it in httphandler

    like:

    context.Request.Querystring ................

    Thursday, September 15, 2011 11:54 PM
  • User1780379888 posted

    thanks for replying. I have tried that but still reads it as null value. I have even created a static class with a property value that reads from a session variable. But still returns null. Is there any way I can make the Httphandler read variables set outside of it that is generated dynamically. Please advise further. Thanks.  

    Friday, September 16, 2011 12:21 AM
  • User1613327603 posted

    public string strTemp
        {
            get
            {
                if (System.Web.HttpContext.Current.Session["strTemp"] != null)
                {
                    return (System.Web.HttpContext.Current.Session["strTemp"].ToString());
                }
                return null;
            }
            set
            {
                System.Web.HttpContext.Current.Session["strTemp"] = value;
            }
        }

    please create above property in common/global/statis class and access this property for store and retrive value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 17, 2011 7:21 AM