8

I have a web form that allows users to upload files while opening a ticket in our system. The upload is done using the "ajax control toolkit" asyncFileUpload control.

The idea is to have files saved to a temp directory and only save them to the permanent location when the user hits the "commit" button.

I would like to be able to get rid of temp files that are no longer relevant (e.g. the user leaves the page open for a month without posting the form).

What would be the best way to not keep temp files forever. Maybe keeping them files in the session? Maybe keeping them in the viewstate?

I could add some JS to the page with a timeout and add some code that runs when the user leaves the page. But these ideas are client side solutions. I wouldn't want someone to tamper with the code and leave me rubbish on my system.

Any other idea?

Thanks, Summerbulb

summerbulb
  • 5,709
  • 8
  • 37
  • 83
  • What do you mean by temporary here ? Is it temp location on server or you completely dont want to download the file ? – Ankit Aug 22 '11 at 17:53
  • @Ankit: The files are only needed if the user decides to post the form. Otherwise, they should be cleared. – summerbulb Aug 23 '11 at 13:02

4 Answers4

8

Maybe keeping them files in the session?

No. This would unnecessarily use large amounts of server memory and users could adversely affect your overall server performance by uploading large files.

Maybe keeping them in the viewstate?

No. This would effectively mean that every post-back would involve uploading (and downloading) the file again for no reason. Application performance would slow significantly and provide a poor user experience (not to mention eat through your bandwidth, which may cost you money).


The files are being stored in a temporary location on the server, right? And the application tracks information about the files in a data store of some kind (I'm assuming a SQL database)? I'd recommend having a separate application to handle the cleanup, especially since you're talking on the scale of a month.

A Windows service or scheduled console application would do the job just fine. It can, at regular intervals, check the directory and the database and, based on whatever rules you give it, perform some clean-up. Maybe even use whatever messaging mechanism may exist in the ticketing system to notify the user of this action, notify an admin, etc.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    If you do have a SQL Server behind this all, consider an SSIS package that runs nightly. you can do almost anything with it and it is trivial to schedule. Beats having another whole app to track. – Bill Aug 22 '11 at 17:22
  • +1. service is the right way to go. An alternative would be to check on the sessionStart event for files that are too old, but the time the server needs to delete files it the time, the users has to wait on his first visit. I'd recommend using a service for this – citronas Aug 22 '11 at 17:37
  • @citronas: Indeed, I'd definitely stay away from any site events for something like this. It's a time-based operation as opposed to a request/response operation (which is what a web application is for). I also like Bill's suggestion for SSIS, I'm just much less familiar with maintaining those. (I'd argue that "having another whole app to track" is also trivial, but that's neither here nor there. The overall point of having a scheduled job on the server remains.) – David Aug 22 '11 at 17:44
  • While the idea of a windows service appeals to me, as we use such services daily, in this case it won't work. The solution must be one that is within the site's scope. Icarus's idea of using On_SessionEnd sound good and I'll look into it. – summerbulb Aug 23 '11 at 13:03
4

My suggestion is to add an On_SessionEnd handler in Global.asax and delete the temp files when the user session ends. You can name the temp files appending the SessionID to them (or prepending the SessionID, whatever you want) and that way you would only delete the files for the user whose session has ended. You could also add an On_Application_End handler to Global.asax to delete ALL files in the temp directory.

Icarus
  • 63,293
  • 14
  • 100
  • 115
1

You could store the files in the user's temp folder. Whether this is a good approach in your particular scenario I can't say for sure:

var dir = System.IO.Path.GetTempPath();

The path will depend on the current (Windows) user, though.

http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx

Rune
  • 8,340
  • 3
  • 34
  • 47
  • how is that possible? you mean save on the TEMP folder of the server? and then again, server has to be restarted before temp folder is cleaned or something like that. – highwingers Nov 14 '13 at 05:10
0

On the commit you can issue a System.IO.File.Move() command. The remaining files can be removed with a service or console app if you have access to the server to do this.

Brownman98
  • 1,053
  • 1
  • 7
  • 17