views:

35

answers:

2

I'm doing some async file uploading with iframes, and I'm wondering if simply pruning the iframe from the DOM is enough to cancel an upload.

This discussion seems to imply that this a poor approach to the problem overall, but annoyingly the JS `expert' refuses to reveal his solution.

Anyway, removing the iframe works in Gecko + WebKit, but I'm wondering if people have experience with other DOM implementations.

A: 

Most likely not. Usually the JS engine works async from the DOM body. Changing the DOM won't stop something that is already in process.

Now, if the JS refernaces a missing part of the DOM, or checks if something exists in order to continue, that might work.

Evan
+1  A: 

That dude in the discussion doesn't have a clue of what he is talking about, IMO. Many call themselves experts, but they are only big guns with empty shells. If you want to do some async file uploading, you could use Flash. I normally don't recommend Flash, but the SWFUpload widget is, IMO, one of the best there is.

http://www.swfupload.org/

Other than that, you could take a look at this, the code is quite ugly, but the author seems to affirm that this is cross-browser.

http://www.webtoolkit.info/ajax-file-upload.html

To answer your question: no, removing the iframe does not guarantee that the download will really stop, or that the download process has effectively been cancelled on the server side. This is because removing the iframe from the DOM does not guarantee that something, somewhere is not still using it, thus is not garbage collected.

Instead of removing the iframe, you can simply set a different target url (ex: "about:blank") that usually sever the connection with the server and ultimately stops any data transfer in progress. You can then remove the iframe after that.

However, I would suggest having a two step process, where you upload the file, along with an hidden field containing some hash value. The file is store in a temp directory on the server. When the upload is complete, you make a second request with the same hash to validate the file upload completion and finish processing the upload. This ensures that if the download is cancelled mid-way, but the file has already been uploaded to the server, that you simply don't finish the process and you can just have another process cleaning up the temporary uploaded files later on.

The first solution is simpler, but requires Flash :(

Yanick Rochon
Thanks, resetting the iframe did the trick: `iframe.src = "javascript:'<html></html>';"`; I feel more confident that this is the right way to do it. WRT to my setup, it's working pretty well with just JavaScript + Nginx, in combination with the Upload and UploadProgress modules.
guns