简体   繁体   中英

ASP.NET - Store Temporary Files

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 (eg 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

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM