hello guys I have been given the task to create a web-app that would allow an end user to load a file to one of our servers. the server would then manipulate the file and then give it back to the end user.The server has IIS7 and .net framework 4 Here is my problem. I have everything figured out except that I'm having a hard time getting rid of the file from the server once i manipulated. Is there a way that i can put a timer say after 30 mins to have the file removed from the server in my code? or is there another solution to doing this? I know the timer suggestion perhaps does not make sense however i cant think of another way to do it so i'm looking for an opinion or another method.
-
How are you giving the file back to the end user? Could the last step of that process delete the file? – mbeckish May 24 '13 at 19:11
-
i like to keep the file for say 10 minutes before deleting im also open to perhaps getting the file from the user store on the server then giving it back to them then delete after say 10 minutes. – Miguel May 24 '13 at 19:17
4 Answers
A timer is a good method to schedule something in the future. You could even reset the timer if the user requests the file again. Just give the timer a delegate that deletes the file when the timer fires.

- 1,812
- 24
- 40
-
can i do that without freezing the interface thought? is there a way to create a timing script? that would execute code after it runs out? – Miguel May 24 '13 at 19:09
-
@Miguel - If the timer runs on the server, how does that freeze the user's browser? – mbeckish May 24 '13 at 19:13
-
-
@mbeckish i dont have admin access to the server i only have access to place files on the server for my specific site. What i was wondering is if i can run a timer when say when the user clicks a button, and it continues to run indefinitely looking for a file. – Miguel May 24 '13 at 19:21
-
@mbeckish You cant do ANYTHING without being able to update the code or install a program.... There is no 'magic something' that will do what you want at the click of a button... – Nicholas Terry Apr 22 '14 at 19:41
-
-
Why not delete it after manipulating? Or whatever the last step in the process is? That would seem to be the best and easiest way.
Depending on volume, it's probably not a great idea to do a single task for each file - rather you should batch them into a queue and have a single thread process the queue.
For instance, you can spin up a background thread in global.asax (perhaps using a Timer) to handle cleanup tasks by comparing file times or something.
Or, as step 1 of the process you could clean any old files. Not exactly the same thing, but may be close enough.
Or, you could abuse the Cache remove callback as a timer.

- 1
- 1

- 84,552
- 17
- 108
- 152
-
i like how your thinking here and i'm not familiar with the global.asax so ill look into that. going back to what you said first is it possiblle to just do it all in memory in other words user loads file i retrieve the data from the file manipulate and some how give the user a download button? – Miguel May 24 '13 at 19:35
-
@Miguel - sure, you could basically store a byte array in the user's session, or just Response.BinaryWrite it out immediately. That sidesteps the issue of storing the file to begin with. – Mark Brackett May 24 '13 at 20:22
Are you able to set up a scheduled task on the server?
This sort of thing is perfect for a simple console app that simply deletes files that have a modified date/time that is older than, say, now.AddMinutes(-10).
The task can run every 10 minutes or so if you like too.
Sometimes best to keep this sort of thing away from your website. Let your site serve your users and create something else to serve your server.. :)
Update If its a heavy-traffic site you could simply delete all old files the next time someone uploads a file. So:
User Selects file to upload, clicks Upload -> you get file -> you delete old files (regardless of who they belong to) -> you manipulate file -> file will be deleted by next users upload...

- 30,247
- 21
- 76
- 127
-
i though about that at first but i was rejected by server administrator, says he would not want anything running as a task, hence the reason for my question. – Miguel May 24 '13 at 19:37
-
he's an ass... :) added an update with another idea. Short from that, you're going to have a hard time for many reasons - App pool might recycle, killing any thread you spur to do this task, is just the first thing that springs to mind... – Darren Wainwright May 24 '13 at 19:45
-
Will he let you install any software on the server? You could knock up something that uses a WatchFolder.. when it detects a new file you can have it delete old files. it's not a scheduled task so he might not be so bad with the idea.. – Darren Wainwright May 24 '13 at 19:49
-
i fee so stupid right now. you are right everytime they load a file i can just delete the file in the folder. :-D. Thank you for that idea!!! so i should only ever have one file in the folder at any given time. – Miguel May 24 '13 at 20:02
-
No worries. Sometimes it's not easy to see the wood for the trees :) – Darren Wainwright May 24 '13 at 20:03
If you can ensure the app stays running all the time you can skip scheduled tasks and use Quartz.NET. In this case, even if it shuts down using quartz would not be that bad -- unless there is something else to this having a few old files hanging around while the app is idle wouldn't hurt.
Insofar as handling this, I would store in an appropriate manner (eg -- your database) an list of the files with a marker for the job being complete as well as deleted. Your quartz task could then grab the files that are marked done but not marked deleted and clean those up. Bonus points for using transactions around the file deletes and updates and logging so you know what happened while the world was sleeping.

- 15,573
- 3
- 34
- 53
-
thank you Wyatt. never knew about quartz but it seems very powerful. I'll have to study that a little more. Thanks for the suggestion. – Miguel May 24 '13 at 20:04