views:

999

answers:

5

Hey Guys, 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.

+1  A: 

Why do you need to save it to the filesystem? Can you just store the image in session? If you have a web farm, and don't have a distributed session cache or a persistent one (State Server, SQL Based, or something else), I can understand that this won't work.

The easiest way to clean up a temp file is to not have to create the file in the first place.

On a side note, I am currently faced with a simillar problem where the user will be able to upload documents during a session and I will need to save them during a wizard process, but then once the wizard is done delete them. Sounds like a solution for temporary items would suite me as well

Edit

Why not export the file out, then read it into memory and store in session (I'm assuming these are small images you will want to watch out if the user can generate lots of large images)

JoshBerke
Unfortunately the program that I have embedded into C# will only allow me to export images as JPEG/PNG/BMP.
Dave
So that program will only export to a file? You can still put a jpeg in memory its just a byte array.
JoshBerke
Sorry, I'm a little new to this =P... you pretty much lost me. The program to my knowledge will only export images (the program is R by the way)
Dave
+4  A: 

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
}
JohnC
This works so long as Session state is stored in process.
JoshBerke
I am not familiar with this method but will give it a try in conjunction with neodymium's post. Thank you.
Dave
+1  A: 

Export to file with a unique name, read it into the session and delete the file. The session data can be streamed out over and over from a page which generates the "image" on the fly from the stored image data.

This would potentially be good if the image "file" is read many times by the client, there is also no clean up, since the clean up happens immediately after the file is read back in.

Cade Roux
I will give this a try, thank you.
Dave
+1  A: 

Is there a possibility the end user may come back and say the picture you delivered is not what they asked for? Then i will think twice about the deletion after use. And rather use a archiving strategy. Else

(i) Use the session variables and clean up on the session_end (ii) Cleanup the file system after x hours (iii) Store the info about the image in a database table. Keep track of the last acccess time and once it's older than 'x' number of hours, delete them. (iv) If you're using SQL Server 2008, store the image itself as a file or binary, and delete them with a DTS. (v) And so on...

Sai Ganesh
Looks like this is a combination of what everyone has said thus far and what I had in mind, thank you very much! I will try implementing the session variables however I am still an amateur and do not quite understand how this works. I will clean out the file system after certain hours for now, and report back to see if I can figure out how to use the session variables more efficiently.
Dave
+2  A: 
I like this idea too, thank you everybody... I have so many options to try =)
Dave