1

I want to remove a temp file from the server after the user close the page (I assumed I don't have this callback on the server by default),

I tried to call a server side method using (ICallbackEventHandler implementation) when the user close the page, but the problem is that the server side method doesn't fire in this case (closing the page), it only response if the page still opened. and I don't prefer to stop closing the page until the server response and send its call back to close the page manually.

In case I must stop closing the page, kindly help me with the best way.

Thanks in advance

Homam
  • 23,263
  • 32
  • 111
  • 187

3 Answers3

4

You can't really know when user is closing the page.

Best you can achieve is using the JS onunload event and in there send AJAX request:

<body onunload="SendRequest();">

And the JavaScript: (jQuery is most simple way)

function SendRequest()
{
    var sURL = "http://localhost/Log.aspx?action=unload&t=" + (new Date()).getTime();
    $.ajax({ url: sURL });
}

This event will be triggered whenever user navigates away from the page: F5, Back, Forward, clicking a link, submitting a form etc... you can improve the code to ignore cases like clicking something in the page let me know if relevant and I'll edit with proper code.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
3

I think it might just be more prudent to delete the file after some time period, say 24 hours, on a recently-accessed basis. That is, all files who haven't been touched in 24 hours get deleted.

Alternatively, you could poll with AJAX, and as soon as you don't receive a request with the user's identifying token within some time threshhold > the polling interval, delete the relevant file.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • Nice idea with the AJAX polling. :) – Shadow The GPT Wizard Jan 04 '11 at 14:08
  • I wonder if you could give more information about Ajax pooling or a link, I haven't found a lot on the internet quickly. – Homam Jan 04 '11 at 14:17
  • It's really not that hard; I think perhaps you're jumping the gun on whatever you're trying to do. Essentially, hit some web URL of yours which forwards to an action/controller/framework class/method of some kind, pull out the user's authentication token, and continuously mark the files associated with that user with a timestamp, or literally `touch` the files. Have another service running on some timer (the interval of which will depend on your needs), and clear out all files who haven't been touched in a certain period of time. – Stefan Kendall Jan 04 '11 at 14:38
0

In a similar situation in my first job, I left the copy on the server and whenever that temp directory was called again from that page, I deleted any files older than 72 hours. Not very good, but was accepted in that situation.

If you need to delete it on the server when you know the user doesn't require it any more, you could use a session listener, see this question.

Community
  • 1
  • 1
StuperUser
  • 10,555
  • 13
  • 78
  • 137