Solved

Delete a file from Global.asa file

Posted on 2001-07-20
15
379 Views
Last Modified: 2011-10-03
I have a webpage that gets a file and copies it to a temp directory on the server (at server.mappath(temp).  THe file is created using the sessionID as the filename.(98855322.pdf for example)  Once the session ends, I want to delete this file, but I'm having trouble doing it.  I don't have a problem creating the file and then opening it in my ASP code, but for some reason When I use the FSO in the sub session_OnEnd() in the global.asa file, nothing happens.  Could I get a code segment that works for sure that will delete the file in the OnEnd of the global.asa so I can see if my problem is perhaps somewhere else.  
This is my pseudo code, as I'm at home and don't have the code in front of me:

Sub Session_OnEnd()

declare vars
create FSO
set folder fso.getobject(folderpath)

set ofiles to folder.files
for each ofile in ofiles
 if instr(1,ofile.name, session.sessionID) > 0 then
     fso.deletefile(folder & ofile.name, true)
end if
next

end sub

I am checking the name of the file, and if it contains the sessionID, then I want to delete it.  This is the behavior I need, but please don't grade my code segment :)  

Thanks
0
Comment
Question by:daron
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 6304011
Good question... I had tried this once myself, never got it to work either...  My thinking was that once the global.asa file is executing the Session_onEnd(), it can't go back into the session and perform any duties on the session.  I would love to hear other experts comments regarding this.

I know Weesong and Silvers are good people to hit-up on global.asa problems.  Wee should be on later this evening and silvers probably in the morning, (one is in Malasia and the other in Russia)
0
 

Author Comment

by:daron
ID: 6304051
I always get good info from Weesiong!
0
 

Author Comment

by:daron
ID: 6304702
mgfranz,  did you find a work around for this behavior?  My purpose for this is when the user finishes viewing these pages that have been created using their sessionID, they need to be deleted from the server to keep from having a ton of files end up existing on the server.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:manihopever
ID: 6304755
I think you know,
A session OnEnd() will fire when we call Session.Abandon or
Session.Timeout is setted.

I you don't heared about the above then try at once.

So when it fires the subroutine it destroys all those sessions.  So don't try for getting SessionId in the OnEnd subroutine.

I accept mqfranz's comment.

thanks,
manie.v

0
 
LVL 33

Expert Comment

by:hongjun
ID: 6304948
There's a bug in IIS and that's sometimes Session_OnEnd does not get fire up. That's really bad.

Moreover, you should not use Session.SessionID. It will get destroyed when OnEnd event is called.

You may use a cookie to store the SessionID and then use it to remove it.

hongjun
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6305445
hongjun is correct.. and mgfranz too.. about the experts :o))

Alas IIS4 and 5 as detected.. have a session timout bug, it is roughly called the "session on end bug" since it is reflected clearly here.. on IIS sessions time out randomly, or never timeout, despit the timeout set in IIS or in the script or the default timeout that is 20 minutes..  this is one of the main reasons why your code never works in the session on end since for that session IIS didn't timout yet.. also this is the main reason that prevent us from storing big portions of info or arrays or recordsets or connection objects in session objects.. since the server resources will be consumed and it will crash on a timely basis.. ok enough descriptions let's cure.. your best way to prevent this is to log the user's traffic in a database.. where you'll store their session id and a timestamp now the main point is on each request you update their corresponding record with the current timestamp to check that they're still browsing the site (on top of each page you can include a file that holds the script to update the timestamp) , now externally you can create an application that will scan/parse this log table and check for records that have a timestamp older than the session timeout.. if found the records will be deleted and their corresponding files.. you can do this also in session on start..

Session abandon will help only if used by catching the onunload event in the body.. you popup an asp page that will fire the session.abandon but alas this is not 100% efficace since the browser can be set not to browse in same context (guess it's called like this).. sessions won't be preserved..

Cookies won't help too since users might not return...

rgrds.. hope got things cleared out..
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6305452
What I have used in the past is something like what silvers stated, I have a onUnload() fire requesting the user to "log out", this fires a script that clears all session parameters and empties the temp database table for the user.  Let me know if you need code, but it's pretty basic stuff...
0
 

Author Comment

by:daron
ID: 6305492
...And I was so excited earlier today for getting the session onend to work at home like I want :)   Well, time to re-think.  Here is where I am:  the user selects a choice on the asp page and a file is copied to a temp directory on the server.  This is necessary for a bunch of reasons I won't get into(user can't view it directly from it's location).  So the name of the file is anything I want to make it.  In my asp page, I am copying the file to the temp directory and then opening a new window that points to that file(so the file can be viewed automatically).  So at some point, These temporary files I have created need to be deleted so as to keep it from filling up the server.   I like the timestamp/db method since it is replicating the onend functionality, but I am under time constraints that may keep me from being able to do that(unless you know of a good sample to work with).  
I also tried your idea mgfranz, but my asp page is reloaded each time a user selects something and the onunload is firing each time.  I'd be content with this if I could work around that.  So I am working on it again tomorrow...

Thanks for the responses.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6305513
Interesting.

hongjun
0
 

Author Comment

by:daron
ID: 6306406
Is there any way when using the OnUnload() method to determine if the page is being reloaded?  I have a form action that points back to this same page, so the page is reloaded 4 or 5 times before a file is copied.  
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6307272
OnUnload you should popup a new window.. XXX sites style.. :o)
BTW I forgot to mention that this bug is random.. on some web servers the session on end works like a charm.. on others it's just stubborn like a monkey.. but from home you might be using PWS..
0
 
LVL 18

Accepted Solution

by:
mgfranz earned 200 total points
ID: 6308378
How about using an onUnload function that runs your Server-side function;

<body onUnload="deleteIt.asp">

<% ' DeleteIt.asp

Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile("c:\temp\"&Session.SessionID&".pdf")
' Delete the files.
f1.Delete
%>

That's all you really need to do.



0
 

Expert Comment

by:cscheepers
ID: 6308386
Try this - it works for me, but only on a single file:

Sub DeleteFile(FileName)
  Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
  FileObject.DeleteFile(FileName)
  Set FileObject = Nothing
End Sub

If this maybe helps, let me know, then we can figure a way to delete multiple files.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6308431
cscheepers, you need to read the comments and the question completely, the problem is not in the FSO.
0
 

Author Comment

by:daron
ID: 6315694
This is currently working fine.  I think it may be better to have the database setup and check it for the expired sessions, but for now this seems to be working every time.  My next test is to see how it does under a load.  That is what I was needing.  Thanks for all the comments.

Daron
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question