Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete temporary files when the user leaves the web page?

Status
Not open for further replies.

jbeteta

Programmer
Jan 6, 2004
2
PE
Hi experts!

This is a difficult question.

How to delete temporary files when the user leaves the web page? Is it possible to do it with .NET? I'm starting to think it is not.

I have WebPage1.aspx. It will make some temporay files (*.gif or *.jpg). What I need is when the user leaves WebPage1.aspx, temporary files have to be deleted.

I try Javascript, using window.unOnload event. unOnload event will call a Logout.aspx that will delete the temporary files using: File.Delete("c:\Inetpub\
'On WebPage1.aspx
<Script language=&quot;javascript&quot;>
function endSession()
{
window.open( &quot;}
</Script>
<body onUnload=&quot;endSession()&quot;>

'On Logout.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
File.Delete(&quot;c:\Inetpub\End Sub

However, on every SUBMIT of WebPage1.aspx, it will raise window.onUnload, which means File.Delete(&quot;c:\Inetpub\ will be executed on every SUBMIT as well. But I just need it be executed when the user leaves the page.

As you can notice, I just want to delete temporary files on a directory when the user leaves the web page. Is it so difficult with .NET?

Be aware that Session_End doesn't work here, because it doesn't fire when the user close the browser. It will fire when time pass out.

Thanks a lot!
 
steve proposed in a recent threat (thread855-736211) the following:

protected void Session_End(Object sender, EventArgs e)
{
//code ...
}

..to activate code behind at the end of a timed out Session (he was considering a close page trigger). You may take a look at that thread. This topic has come up a few times over the last few months - you might want to do a quick Keyword Search also.
 
Isadore,
of course I've tried Session_End, but it won't work, because Session_End will ONLY fire when time pass out, usually 20 min. which is set on web.config. That's why I say it's not an easy question.
Anyone can try the following:
1) Set mode variable on web.config
<sessionState
mode=&quot;InProc&quot;
stateConnectionString=&quot;tcpip=127.0.0.1:42424&quot;
sqlConnectionString=&quot;data source=127.0.0.1;Trusted_Connection=yes&quot;
cookieless=&quot;false&quot;
timeout=&quot;20&quot;
/>
2) Start a Session variable.
3) Put some code on Session_End and then check if that code is executed when the user click [X] on the browser.
4) It will never raise.
 
Perhaps you should not be creating the files at all - just create the images in memory and send them to the html output. Then when garbage collection kicks in, the image data gets destroyed along with the class.

Just a thought.

David
[pipe]
 
Dragonwell is right! You shouldn't be creating temp files just to have the user view them. I had the exact issue and I was about to settle on a timed garbage collection. I found a tread on one of the groups on the exact paradigm.
You have your image object (ie img tag). You set the source to a a web page like show.aspx. In the Page_Load event on the show.aspx page you have three lines.

Bitmap test = new Bitmap(&quot;your image file&quot;);//this could come from the query string (part of you img src
tag).;
Response.ContentType=&quot;image/Gif&quot;;
test.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
test.Dispose();//for file locking issues;

I hope that this helps.
Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top