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

J

Jorge_Beteta

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/wwwroot/temp/.gif").

'On WebPage1.aspx
<Script language="javascript">
function endSession()
{
window.open( "http://ServerName/Logout.aspx");
}
</Script>
<body onUnload="endSession()">

'On Logout.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
File.Delete("c:\Inetpub\wwwroot\temp\*.tmp")
End Sub

However, on every SUBMIT of WebPage1.aspx, it will raise
window.onUnload, which means
File.Delete("c:/Inetpub/wwwroot/temp/*.gif") 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?

Thanks a lot!
 
M

Mark Fitzpatrick

ASP.Net itself won't really know when a user leaves a page because, as far
as it's concerned, once the client has finished downloading the page they're
not connected to the server anymore. You could try making use of something
like the Session_End event that could do some cleanup. That would guarantee
that 20 minutes (or however much you set the sessions timeout to be) after
the last page that a user visits the files will be deleted. I'm not sure if
this approach will definitely work since not all objects are available in
the Session_End event, but it would provide you with an easy mechanism.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
D

Dan Brussee

Two suggestions.

1. Instead of creating temp graphic or data files, stream them to the
browser. Many examples on how to do this abound.

2. If you absolutely have to handle temp files, the generation code
should be able to clean up itself. The Page object will raise an
Unload event when it is finished. From a handler of that event, you
should be able to clean up. Of course this will mean you cannot cache
anything on that needs cleanup, but I guessing this is not a problem
since if the page is refreshed, the graphics, etc might need to be
regenerated. No?
 
J

Jorge_Beteta

Thanks for your suggestions, Dan.

About using Page.Unload, I have to say that Page.Unload fires EVERY
SUBMIT, not only when the user close the page or leaves the page.

I use WebControls.Image to show gif files on the browser. In order to
achieve that I use Filestream:

strImgFilePath = "c:\Inetpub\wwwroot\temporal\"
strImgFileName = "random_number.gif"
'oPhoto As Byte(), is data retrieved from field/table
Dim oFileStream As FileStream = File.Create(strImgFilePath &
strImgFileName, oPhoto.Length)
oFileStream.Write(oPhoto, 0, oPhoto.Length)
imgControl.ImageUrl = "temporal\" & strImgFileName
oFileStream.Close()

As you see, I have to create the gif file in order to FileStream it.
Is it a way to use FileStream wihtout creating the gif file on a
carpet?

Thanks,
Jorge
 
S

Shiv Kumar

Your ImageUrl property should point a another .aspx page (sending it any
addtional parameters if required) where in all you do is extract the data
required from your database (using the parameters sent) and stream the data
straight out to the browser using the Response.OutputStream
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top