1

I'm using an uploader applet that's sending the files to a server side script in php. I want to make sure that, if the user closes the page before the files finish uploading, that the files are deleted from the server since they couldn't be dealt with properly.

The issue comes from the fact that I have one site trying to call two server side scripts at the same time.

If I were to close the page without running the uploader applet, the file deletion script is called with

$(window).bind('beforeunload', function(){
    $.post("cancel.php");
});

If I run the uploader applet, however, the event won't trigger and the files just sit on the server. Is there a way to stop the upload script to allow for the cancel script to run? If not, is there a way to wait for the upload script to stop before running the cancel script and before closing the window?

EDIT: Ah, the joy of googling the answer to your question only to see that your question on stack overflow is the top hit on google...

EDIT 2: So after rearranging the order of my calls in my beforeunload event, I've successfully been able to call the scripts that I want when I want. I had to make sure I called cancel() on the applet before attempting to call the other script.

Scott M
  • 417
  • 5
  • 16

1 Answers1

1

Rather than running a script to dynamically delete the file, why not transfer a checksum (perhaps an MD5 checksum) over to the server and check for incomplete file transfers?

Reference: http://php.net/manual/en/function.md5-file.php

This way, you don't need to rely on an entire script (and the HTTP requests) required for deleting the scripts - and use a database to index the unchecked files appropriately.

Enjoy and good luck!

Daniel Li
  • 14,976
  • 6
  • 43
  • 60
  • The uploader can take multiple files at a time, meaning some will be completely uploaded when the user closes the window. This won't take care of those files. – Scott M Jul 17 '12 at 20:44
  • This is a good solution because a user could easily defeat your JavaScript code and prevent it from running. – wecsam Jul 17 '12 at 20:44
  • 1
    @ScottM Can't you generate the MD5 sum for each file? – wecsam Jul 17 '12 at 20:45
  • Hi ScottM, you take the MD5 sum of each file instead of each session in order to ensure file transfer success. This way, any incomplete files within a set will be the only ones affected. – Daniel Li Jul 17 '12 at 20:46
  • I don't know if I'm understanding this solution entirely or not since I'm not familiar with checksums. Won't this still require a request to the server while my upload script is running? – Scott M Jul 17 '12 at 20:54
  • You can run a server-side script periodically to check all files that are not bound by a script but are done uploading. This way there is no need to send a secondary request. – Daniel Li Jul 17 '12 at 20:56
  • 1
    how would you generate a checksum of the file on the client computer? The applet might be able to, but he is using a pre-made java applet. javascript/html has no access to files on the client computer, even after the user has selected the file in an upload box. Other than that, when would you check the if the checksum is valid? if the file hasn't completed upload (still being uploaded, not cancelled) the checksum wouldn't match. You would need to check that the upload was finished some how and then check the hash. – Jonathan Kuhn Jul 17 '12 at 21:00
  • @JonathanKuhn None of this is on the client-side. The checksum script works on the server-side and periodically scans files that are not bound to the upload script. – Daniel Li Jul 17 '12 at 21:04
  • @HopeIHelped how would you "transfer a checksum" then? Or am I missing something in the post in that a "user" is "uploading" a file? generally if a user is uploading a file, that would be the client. – Jonathan Kuhn Jul 17 '12 at 21:11
  • http://stackoverflow.com/questions/768268/how-to-calculate-md5-hash-of-a-file-using-javascript – Daniel Li Jul 17 '12 at 21:13
  • @HopeIHelped thanks, i hadn't seen that before. So it would work for newer browsers and older browsers wouldn't work. – Jonathan Kuhn Jul 17 '12 at 21:15
  • I'm using [JFileUpload](http://www.jfileupload.com) as my applet, as stated in a comment on the post. One of my concerns is that my upload script doesn't "bind" anything to it. It goes chunk by chunk for each individual file. If the applet is in the middle of uploading file 3 out of 5 and the checksum script runs, what happens to files 1 and 2? Also, what happens if the checksum script runs and there are two users uploading from different sessions? I think @JonathanKuhn is correct and I'd rather not use a checksum script. It's a bit over my head anyway. – Scott M Jul 17 '12 at 21:17
  • @HopeIHelped The answer there suggests that it may not work in Safari 5, but that is the main environment that the people I'm building this site for use. On top of that, I'm not at all familiar with how to implement the type of server side script you're suggesting, as stated before. – Scott M Jul 17 '12 at 21:20