views:

271

answers:

3

Hi,

I'm building a small webapp on win2003 with asp.net 2.0. I need to create small files that i suppose to be accessable by for the web and the files is suppose to have 10 minute expiration time. I didn't find any support for this in the framework and my other solution that came to mind was to have a schedule job that cleans the file ever X minute. But somehow I think I'm not the first one to experience this problem and wondering if there are any other smart solutions.

Thanks in advance.

A: 

Since it's small, and if you want to keep it simple, just check on every N requests to the website. If you think the number of files to clean could get large, stop cleaning after you find a few to clean and let the next request clean up a few more. The advantage is that you don't have to manage an external job (and install it and keep it running, etc).

Probably not a good idea for a big site.

Lou Franco
A: 

Is the purpose to control file access to remote users?

You could create a Windows service or scheduled console app to periodically remove expired directories/files. Alternatively, you could have your web app manage the file response rather than linking directly to the files from your web page. Once the file is past a certain age just code your app to simply not return the file.

Here is something similar to what I have done in the past

protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = @"c:\SecureDirectory\DownloadableFile.txt";
            int exipireMinutes = -10;

            if (System.IO.File.Exists(fileName) && System.IO.File.GetCreationTime(fileName) >= DateTime.Now.AddMinutes(exipireMinutes))
            {
                byte[] fileBinary;
                using (System.IO.FileStream fs = System.IO.File.Open(Server.MapPath(fileName), System.IO.FileMode.Open))
                {
                    fileBinary = new byte[fs.Length];
                    fs.Read(fileBinary, 0, Convert.ToInt32(fs.Length));
                }
                Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(fileBinary);
                Response.End();
            }
        }
William Edmondson
+2  A: 

Without knowing more about why you are doing this, I think your solution of an external program to clean the files out is the best solution. An alternative might be to store the "files" in a database and create a handler that retrieves the data from the database, but only if the "file" is not expired. You could then schedule a clean up task on the database much less frequently and still only serve up the "files" that are not expired. Creating them on disk would force you to do the clean up on a regular schedule to ensure that expired items are no longer available.

I suppose you could also do the handler with on-disk files and check the creation date before serving the file up. In this case the folder containing the actual files would need to be outside the web site or in a restricted area that prohibits direct access from normal users.

tvanfosson