Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Delete server-side file upon browser close

LEGEND ,
Dec 02, 2010 Dec 02, 2010

Hello, everyone.

Is there a way to delete a server-side file if the browser is closed?

I  have a pop-up window that allows a user to send an email to someone that will  have a file attachment. Currently, the file data is read from a VARBINARY(MAX)  column in a database and created/saved in a folder that is created for it (with  a unique name for the directory) when the pop-up first loads. When the form is  submit, the file is emailed then deleted and the directory for it is  removed.

If the user changes his/her mind and closes the pop-up without  hitting submit, the file and directory are left. I'd like to be able to delete  the file and remove the directory if the pop-up is closed without hitting  submit.

Can this be done with JavaScript? Or can this only be done with ColdFusion?

If only  server-side scripting can do this, how can I trigger the script to run when the  browser is closed?

Thanks,

^_^
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 02, 2010 Dec 02, 2010

I would think that J2EE sessions are the only types of sessions that know when the browser is closed, so you would have to use J2EE session vars, and then in your onSessionEnd, go through the process of terminating the file.

Note that in your onSessionEnd, you cannot reference, SESSION scope, since it doesn't exist anymore.  You'll have to reference the session object argument that you can pass in as an argument.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 02, 2010 Dec 02, 2010

No J2EE sessions DO NOT know when the browser is closed.

All that happens with J2EE sessions is that the cookie the browser was using is thrown away when the browser is closed.  Thus if a new browser is opened to this site, a new cookie will be generated with a new session.  But the old session data is still there until the session time out period expires.  Because browsers don't go around to all the web sites you may have visited since starting up saying something like "I'm closing now".

P.S. Regular, old fashioned, CFID and CFTOKEN cookies can also be made to be memory cookies that are thrown away when the browser closes like the JSESSIONID cookie used with J2EE sessions.  It's just that the JSESSIONID is a memory cookie by default.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 02, 2010 Dec 02, 2010

We use a different approach.  We have a scheduled job that looks in designated directories and deletes anything more than 30 days old. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 02, 2010 Dec 02, 2010

It depends on how time sensitive you want this to be.

The easy way would be to put the code to clean up these directories in the OnSessionEnd() event of an Applicaiton.cfc.  This event will fire when the session ends after the time out period expires (20 minutes by default).

If you want something to actually happen when the popup is closed.  Since it is a JavaScript popup, there are JavaScript events fired when it closes.  You could write code that executes when this event happens.  That code could easily use AJAX or some other method to make a request to a 'cleanup.cfm' page which wuld do the desired task.

If I where to do number two, I would still do number one to cover all the cases where the something happens that does not fire off the event, like the user closes the entire browser or have their browser or computer crash, etc.

And if you are doing number one, you should ask yourself if there is enough benifit to number two to bother with all the extra work to create and maintain another section of code to have this happen twenty minutes sooner then it is going to happen anyway.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 02, 2010 Dec 02, 2010

Session variables have been disabled, so I don't know if #1 would work.

#2 is more along what I'm looking for, but the few references I've found for file manipulation using JavaScript haven't worked.  I've tried the "onBeforeUnload", but even if it did work, it would delete the file as soon as the user clicked Submit.

^_^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 02, 2010 Dec 02, 2010

WolfShade wrote:

I've found for file manipulation using JavaScript haven't worked

You don't want to manipulate the file with JavaScript.  The file(s) aren't anywhere where JavaScript can manipulate them.  JavaScript is going to run on the client, the files are on the server.  They do NOT share a file system.

What you want to do is manipulate the REQUEST.  You use JavaScript to make a request to the server.  This request is for a ColdFusion template that will delete the desired files.  This can be accomplished pretty simple with either AJAX or small inline frames that make a request to the server.

From the server side, it does not know or care that the request is from a JavaScript function on the client rather than the more common anchor click, form submit or URL input.  It just does what it has been coded to do when it is requested.

But since the request is from a JavaScript function, the user interface doesn't have to change in any way when doing this request.

But YOU really need to consider what to do when all that fancy JavaScript DOES NOT WORK.  This can happen for many reasons.  Users have JavaScript disabled.  Users close the browser in a manner that does not trigger the JavaScript event.  Users just turn of the system or it crashes.

If you don't have sessions enabled, then a scheduled task that runs on a certain period would probably be the next best option.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2010 Dec 02, 2010

You can't really know when the browser is closed. You only know when the browser made its last request. You can use onSessionEnd to do this sort of cleanup, as that will run when the session has timed out (by default, 20 minutes after the last request from the browser).

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on

GSA Schedule, and provides the highest caliber vendor-authorized

instruction at our training centers, online, or onsite.

Read this before you post:

http://forums.adobe.com/thread/607238

Dave Watts, Eidolon LLC
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 03, 2010 Dec 03, 2010
LATEST

Why don't you have the file uploaded to the temporary directory on the server ( i.e. - GetTempDirectory() )? It'll eventually be removed on its own.

If you're wanting to go with the JavaScript option, you could use a variable in your JavaScript to determine whether or not to remove the file when the window is closed.

Example:

<script language="text/javascript">

     doDeleteOnClose = true;

     // Function called before closing the window

     function deleteFileOnClose() {

          if (doDeleteOnClose) {

               // Code to delete the file

          }

     }

     // Function called before submission of the form

     function doBeforeSubmit() {

          doDeleteOnClose = false;

          return true;

     }

</script>

<form onSubmit="javascript: doBeforeSubmit();">

     <input type="submit" name="submit" value="Submit" />

</form>

--

Daniel Jeffery

Vivio Technologies

http://www.viviotech.net/

http://www.linkedin.com/in/jeffda

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources