Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "temporarily" store images on web server per session in ASP.NET and C#

I've been looking around for quite a while and feel that I have a particularly unique situation.

I am generating an image from a 3rd party program (which I embedded into my C# app) which is saved to the file system on my web server. Do you know of any efficient ways to dispose of these images after a particular session is completed?

For Clarity: Initially I was thinking about a folder (for my images stored by session ID) that has its contents automatically deleted every 24 hours or so. I don't like this idea because it's not really scalable.

I also looked at the ASP.NET dynamic image generation from this website. http://www.sitepoint.com/article/generating-asp-net-images-fly/

however this seems a little overkill because my image needs to be saved to the file system one way or another. If I save the image as a static name then generate a bitmap from it using the aforementioned method I am worried that concurrent usage could break the application.

This leaves me with saving the file names with the session ID in the name, however I can't find any efficient way of cleaning up the images after the session ends.

Thanks!

-Dave

Update: I like pretty much all the solutions presented so far (I don't think the database one will work because of the way I am exporting images) I appreciate all of your help... I will start working on implementing these and will report back which seems to work best!

Update2: I ended up randomly generating image names, storing them, then removing them at the end of the session, thanks for all your help. All the methods presented were excellent suggestions though. For this particular situation though this method works the best.

like image 419
Dave Avatar asked Jun 30 '09 19:06

Dave


2 Answers

How about adding a Global.asax file to your website, and in the Session_End event, you remove the file.

    protected void Session_End(Object sender, EventArgs e)
{
    // Remove file
}
like image 144
JohnC Avatar answered Oct 19 '22 22:10

JohnC


Track the temporary file names in a list and remove them in the Session_End event or in the load or init event of every page check for session timeout programmatically and remove the files.

Something like:


if (Context.Session != null)
{
  if (Session.IsNewSession)
  {
    // delete temporary files
} }
like image 2
user79755 Avatar answered Oct 19 '22 21:10

user79755