views:

1067

answers:

4

I have an ASP.NET MVC view that uses jquery.Uploadify to post files to one of my controllers for use in my application and one of the side effects I noticed with Uploadify is that when the Flash file Uploadify uses to submit files to the server posts to my controller it gets its own SessionID from ASP.NET. This would be fine, of course, if my Upload controller didn't use the Session to store a list of files that have been uploaded by the current user for future manipulation by my application... So given this issue, after I upload files with my View, the current user's session does not contain any of the files that were just posted.

Any suggestions on how to achieve what I want without relying on Sessions (and, preferably, without a database)?

A: 

Since Uploadify is purely a front end script, I don't understand why it would be getting a session from ASP.NET. I also don't fully understand what your particular problem is.

If your problem is that once the files are uploaded, the user can't see them on the screen, then I would suggest figuring out a method displaying the list of files that is independent of Uploadify. If it can, have it send an ID token along with the files and then grab the data needed to show the list from a database.

Chris Johnston
Essentially that's the problem, aye. I considered caching the uploads to the database, but that seems like trying to hammer a nail with a truck...
Nathan Taylor
+1  A: 

Maybe a static hashmap which key is the user:ip of the client? The value will be whatever object you want to stored across the different Sessions.

Freddy
That ain't bad at all actually. Thank you sir.
Nathan Taylor
Of course that wouldn't work so hot in a scenario like a user at a coffee shop- although if I were to pass the session ID in with the upload operation and then use a HashMap of session IDs I could just explictly use the provided session ID. Is it possible to get a Session object for a given Session ID?
Nathan Taylor
A different session ID indicates that a new session is in place, the old one is no longer available (well not on the current session) unless you save the previous one. Session is an object and you could store it on your hash, however I think that workaround will cause more problems than the ones it solves.But what is the case you are referring to at the coffee shop? User change IP address?
Freddy
In the coffee shop scenario there would be multiple users with the same IP Address thus making my UserTable bridge multiple users. If instead I use the Session ID I can guarantee the value is going to be unique. I added my "UserTable" implementation to the above post. It seems to do what I want although I haven't rigorously tested it yet. Obviously, I don't intend on replacing Session with UserTable because I really only need the UserTable in this one scenario. All the same, I am storing the UserTable in the BaseController for all to access.
Nathan Taylor
OK, understand. That is why I suggested the 'user:ip' as they key for the hashmap. I think that combination should be unique on 99.99% of the cases.
Freddy
BTW, user means the user which is connected on your session, so your key will be a string like "freddy:207.95.30.3"
Freddy
Ahh I see what you're saying. That would work also.
Nathan Taylor
A: 

One thing to cross check--did you make the session "live" by adding some data to it before going to uploadify? ASP.NET regenerates sessions until it has data in the session.

Wyatt Barnett
Aye, the session is in fact live. The problem pertains to SWFUpload, not just me. :)
Nathan Taylor
Gotcha. I'd use a http debugger (such as Fiddler2) and see what is going on on the wire. Something don't quite add up here. At least according to my spidey sense.
Wyatt Barnett
A: 

This is the solution I came up with. I haven't done much testing, but it seems to be an acceptable alternative to Session in my current scenario. I will use the Global.asax's Session_End/Session_Start to ensure rows are created and removed as needed.

public class UserTable : Dictionary<string, Dictionary<string, object>>
{
 public new object this[string key]
 {
  get
  {
   object value = null;
   if (HttpContext.Current != null)
   {
    var sessionId = HttpContext.Current.Session.SessionID;
    if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
     value = base[sessionId][key];
   }
   else
    throw new Exception("No HttpContext present.");
   return value;
  }
  set
  {
   if (HttpContext.Current != null)
   {
    var sessionId = HttpContext.Current.Session.SessionID;
    if (!ContainsKey(sessionId))
     Add(sessionId, new Dictionary<string, object>());
    if (!base[sessionId].ContainsKey(key))
     base[sessionId].Add(key, value);
    else
     base[sessionId][key] = value;
   }
   else
    throw new Exception("No HttpContext present.");
  }
 }

 public object this[string sessionId, string key]
 {
  get
  {
   object value = null;
   if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
    value = base[sessionId][key];
   return value;
  }
  set
  {
   if (!ContainsKey(sessionId))
    Add(sessionId, new Dictionary<string, object>());
   if (!base[sessionId].ContainsKey(key))
    base[sessionId].Add(key, value);
   else
    base[sessionId][key] = value;
  }
 }

 public void Add(string sessionId)
 {
  Add(sessionId, new Dictionary<string, object>());
 }

 public void Add()
 {
  if (HttpContext.Current != null)
   Add(HttpContext.Current.Session.SessionID);
  else
   throw new Exception("No HttpContext present.");
 }

 public new void Remove(string sessionId)
 {
  base.Remove(sessionId);
 }

 public void Remove()
 {
  if (HttpContext.Current != null)
   Remove(HttpContext.Current.Session.SessionID);
  else
   throw new Exception("No HttpContext present.");
 }
}
Nathan Taylor