Answered by:
Accessing Session Variables In HttpHandler

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.Wednesday, September 14, 2011 7:19 PM
Answers
-
User-248200860 posted
HI
Write a Static Class to catch and maintain Seesion variables like
follwing class
public class UserSession
{
public UserSession()
{
}
public static int ID
{
set { HttpContext.Current.Session["Id"] = value; }
get { return (int)HttpContext.Current.Session["Id"]; }
}}
when u want assisgn a variable to Session
Usersession.Id=10;
in ur handler clas
can u Use this variable
i Hope it wil be helpful to My dear frnd
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 15, 2011 2:14 AM
All replies
-
User896700311 posted
Hi,
modify your code this way:
public class Upload : IHttpHandler, IRequiresSessionState { ....
Any doubt, post your comment.
Wednesday, September 14, 2011 8:16 PM -
User1780379888 posted
Thanks for replying Segundo. Still does not work after using IRequiresSessionState. Please advise further. Thanks again.
Wednesday, September 14, 2011 10:43 PM -
User-248200860 posted
HI
Write a Static Class to catch and maintain Seesion variables like
follwing class
public class UserSession
{
public UserSession()
{
}
public static int ID
{
set { HttpContext.Current.Session["Id"] = value; }
get { return (int)HttpContext.Current.Session["Id"]; }
}}
when u want assisgn a variable to Session
Usersession.Id=10;
in ur handler clas
can u Use this variable
i Hope it wil be helpful to My dear frnd
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 15, 2011 2:14 AM -
User1780379888 posted
Thanks for replying Venkatesh. As suggested, I created a static class for UserSession and called the variable but still returns null in the httphandler page. To test if my static class worked, I called the variable in my .aspx page and it does. Don't really know why the .ashx page is not reading any external variables. Please advise further. Thanks for helping.
Following is my code
UserSession.cs (In the App_Code folder)
using System; using System.Collections.Generic; using System.Linq; using System.Web; public static class UserSession { static UserSession() {} public static string FID { set { HttpContext.Current.Session["fid"] = value; } get { return (string)HttpContext.Current.Session["fid"]; } } }
Upload.ashx
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) { HttpContext.Current.Session["fid"] = System.Guid.NewGuid().ToString(); ... } } }
AutoUpload.aspx.cs
<%@ 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 = UserSession.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; } } }
Thursday, September 15, 2011 5:33 PM -
User-1022976856 posted
Hi @eason2004
I exactly have same issue. Have you found solution for this, if yes kindly share it
Thursday, December 22, 2011 1:02 AM