Delete Temp File after N seconds
-
Im using ASP.net 2.0. I have a need to temporarily save an image to disk, and then want it automatically deleted within a minute. Im a gernating dynamic pdfs, with dynamic charts embeded in the PDF. The charts need to be temporarily saved as an image on disk with Guid.NewGuid().ToString() + ".jpg"; for example as the file name. I thought about using the Web.Cache with a Delegate CacheItemRemoveCallBack setting abosulte expiration to expire in 60 seconds, then when the listener is fired, delete the file. Problem is, that method will not work. because the file has a unique key in cache (using the file name as the cache key which is from NewGuid) , it will never be fired beacause that cache key will never be fired again. the same image will never be generated twice. For time-based dependencies, the callback is not fired when the time is up, but rather the next time the cached item is accessed. So cant depend on these to act as a timer for me to remove an item in N seconds. I would rather not get into the business of schedule task manager to run and delete files etc... I suppose that in this handler (btw, its in an httpHandler) I could just do a check on time stamps and remove files older than N secs, I would rather not slow the request down though. For some reason, im thinking Threading namspace / Timer object (maybe overboard). Does anyone have any suggestions? let me know if you need more info. Thank You all greatly in advance Shawn
-
Im using ASP.net 2.0. I have a need to temporarily save an image to disk, and then want it automatically deleted within a minute. Im a gernating dynamic pdfs, with dynamic charts embeded in the PDF. The charts need to be temporarily saved as an image on disk with Guid.NewGuid().ToString() + ".jpg"; for example as the file name. I thought about using the Web.Cache with a Delegate CacheItemRemoveCallBack setting abosulte expiration to expire in 60 seconds, then when the listener is fired, delete the file. Problem is, that method will not work. because the file has a unique key in cache (using the file name as the cache key which is from NewGuid) , it will never be fired beacause that cache key will never be fired again. the same image will never be generated twice. For time-based dependencies, the callback is not fired when the time is up, but rather the next time the cached item is accessed. So cant depend on these to act as a timer for me to remove an item in N seconds. I would rather not get into the business of schedule task manager to run and delete files etc... I suppose that in this handler (btw, its in an httpHandler) I could just do a check on time stamps and remove files older than N secs, I would rather not slow the request down though. For some reason, im thinking Threading namspace / Timer object (maybe overboard). Does anyone have any suggestions? let me know if you need more info. Thank You all greatly in advance Shawn
Why not use a Windows Service that will delete periodically?