Callbacks out of order, IE triggers onbeforeunload

Upload core product.
Post Reply
scottm
Posts: 2
Joined: Wed Jul 25, 2012 9:21 pm

Callbacks out of order, IE triggers onbeforeunload

Post by scottm »

I'm using JFileUpload for an asset management system and I want to use the onbeforeunload event to make sure everything is squared away as it should be if they exit the window as the uploader is working. The event cancels the upload and calls another script to handle the files already on the server.

I have it working in every browser except for IE (of course), which always triggers the onbeforeunload event at the very start of any and every upload. I've tracked down the culprit, so far as I can tell. In my event log, I'm getting certain callbacks out of order. Here's a couple examples.

Code: Select all

JSAppletInitialized(JFileUpload 2.7 Build FINAL.001)
JSTransferTriggered(CookieMonster.jpg|PossiblePageTabImage.png|PossiblePageTabImage2.png)
JSSplitTriggered(CookieMonster.jpg,66989,67108864,1)
JSSplitCompleted(1)
JSTransferStarted(CookieMonster.jpg,66989)
JSTransferCancelled()
JSSplitStarted(1,66989)

Code: Select all

JSAppletInitialized(JFileUpload 2.7 Build FINAL.001)
JSTransferTriggered(Beethovens Symphony No 9 - Copy.mp3|Beethovens Symphony No 9.mp3|Sad Ending Trio speed.mp3)
JSSplitTriggered(Beethovens Symphony No 9 - Copy.mp3,605654,67108864,1)
JSSplitCompleted(1)
JSTransferStarted(Beethovens Symphony No 9 - Copy.mp3,605654)
JSSplitStarted(1,605654)
JSTransferCancelled()
JSTransferCancelled()

Code: Select all

JSAppletInitialized(JFileUpload 2.7 Build FINAL.001)
JSTransferTriggered(gmw3059_groups.sql|some_users.sql)
JSSplitTriggered(gmw3059_groups.sql,3736495,67108864,1)
JSSplitCompleted(1)
JSSplitStarted(1,3736495)
JSTransferCancelled()
JSTransferCancelled()
JSTransferStarted(gmw3059_groups.sql,3736495)
JSTransferCompleted(gmw3059_groups.sql.1)
JSSplitTriggered(some_users.sql,9355781,67108864,1)
JSSplitStarted(1,9355781)
JSSplitCompleted(1)
JSTransferStarted(some_users.sql,9355781)
JSTransferCancelled()

Code: Select all

JSAppletInitialized(JFileUpload 2.7 Build FINAL.001)
JSTransferTriggered(oats_14.mov)
JSSplitTriggered(oats_14.mov,2077155,67108864,1)
JSSplitCompleted(1)
JSTransferStarted(oats_14.mov,2077155)
JSSplitStarted(1,2077155)
JSTransferCancelled()
JSTransferCancelled()
In the case of the third example, the upload continued despite the upload being "cancelled" twice, so I had to manually cancel it.

The main pattern is that the Split callbacks are in the order "Triggered" -> "Completed" -> "Started" which makes no sense, and I think that's the source of the problem. Every so often JSTransferStarted will happen before JSSplitStarted is called back, as in the fourth example.

This only happens for the first file in any upload and only in IE. The file does make it to the server uncorrupted in any way, but this makes multi-file uploads impossible.

Now, there are two things I need to know.

1. Is there anything stemming from these mis-ordered callbacks that could trigger the onbeforeunload event? MSIE uses some pretty stupid triggers, like document.write, so it's believable that there's something triggering the event.
2. Is there any way to prevent this from happening?

Thank you.

User avatar
support
Posts: 1503
Joined: Sun Jan 27, 2008 6:19 pm

Re: Callbacks out of order, IE triggers onbeforeunload

Post by support »

Are you using HTTP or FTP upload?
If HTTP, are you sure you have "Connection","Close" HTTP header enabled in the server-side script?
If not it could explain why cancel does not work as expected.

scottm
Posts: 2
Joined: Wed Jul 25, 2012 9:21 pm

Re: Callbacks out of order, IE triggers onbeforeunload

Post by scottm »

support wrote:Are you using HTTP or FTP upload?
If HTTP, are you sure you have "Connection","Close" HTTP header enabled in the server-side script?
If not it could explain why cancel does not work as expected.
I am using HTTP, yes. I'm not sure what you mean by "Connection","Close" headers, though. I'm pretty new to web programming, so forgive my ignorance. I'm using a PHP script for the server side. How would I go about making sure all the headers are enabled? I don't see any mention of connection or close in the script I'm using

EDIT: so I've found the function

Code: Select all

header("Connection: Close");
which seems to be what you're talking about. Is this correct? Adding that to my script didn't fix the issue.

EDIT 2: Decided to elaborate on how I'm defining and implementing onbeforeunload in this context.

Code: Select all

	function JSTransferTriggered(filelist) {
		log('JSTransferTriggered(', filelist, ')');
		jQuery(window).on('beforeunload',function(){
			//disable JFileUpload
			if(/*This wasn't called by triggering cancel/leaving as they should*/){
				//Cancel the upload
			}
		});
	}



	function JSTransferCancelled() {
		log('JSTransferCancelled()');
		jQuery.ajax({
					   'async': false,
					   'type': 'POST',
					   'url': 'cancel.php'				
					});
		//close the window
	}
The general idea is to catch the window closing event and take care of any files that were orphaned in the temp folder if they leave the page when they shouldn't. It also disables the applet once the page is gone since use of the applet away from the intended page (via dragging it) can cause a couple problems.

All of the logs are listed in order when I disable the onbeforeunload listener, but once I introduce that function, the logs go out of order and starting an upload triggers the onbeforeunload event.

User avatar
support
Posts: 1503
Joined: Sun Jan 27, 2008 6:19 pm

Re: Callbacks out of order, IE triggers onbeforeunload

Post by support »

header("Connection: Close"); is the one I was talking about.
I see you're using 2.7, could you update to release 2.9?

Post Reply