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

Discussion in 'ASP .Net' started by Jorge_Beteta, Jan 6, 2004.

  1. Jorge_Beteta

    Jorge_Beteta Guest

    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!
     
    Jorge_Beteta, Jan 6, 2004
    #1
    1. Advertising

  2. 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

    "Jorge_Beteta" <> wrote in message
    news:...
    > 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!
     
    Mark Fitzpatrick, Jan 6, 2004
    #2
    1. Advertising

  3. Jorge_Beteta

    Dan Brussee Guest

    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?


    On 6 Jan 2004 13:51:31 -0800, (Jorge_Beteta) wrote:

    >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!
     
    Dan Brussee, Jan 7, 2004
    #3
  4. Jorge_Beteta

    Jorge_Beteta Guest

    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

    Dan Brussee <> wrote in message news:<>...
    > 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?
    >
    >
    > On 6 Jan 2004 13:51:31 -0800, (Jorge_Beteta) wrote:
    >
    > >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!
     
    Jorge_Beteta, Jan 11, 2004
    #4
  5. Jorge_Beteta

    Shiv Kumar Guest

    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

    --
    Shiv R. Kumar
    http://www.matlus.com
     
    Shiv Kumar, Jan 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TmVvIFRoZSBPbmU=?=

    Can I safely delete Temporary ASP.NET Files?

    =?Utf-8?B?TmVvIFRoZSBPbmU=?=, Oct 15, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    48,256
    Nora.brown
    Apr 3, 2010
  2. Manish
    Replies:
    4
    Views:
    10,973
    sreetak
    Jun 19, 2008
  3. Mike Dee
    Replies:
    0
    Views:
    383
    Mike Dee
    Mar 2, 2006
  4. Alex
    Replies:
    1
    Views:
    86
    Martin Honnen
    Dec 28, 2006
  5. windandwaves
    Replies:
    4
    Views:
    152
Loading...

Share This Page