Question

I'm working on a file uploader, where about 10 files upload at the same time via a for loop.

Now I am trying to create a cancel button to cancel ALL the uploads, however with my current code, only the very last upload will cancel.

I've included my boiled down code, but basically its a loop which goes through an array of images (theAttach) and for each image it sets up an xhrAttach to send the images. So about say 10 images start uploading at the same time.

If a cancel button is pressed, I run the command xhrAttach.abort(); but only the very last image aborts.

Any ideas?

                for (var i=0;i<theAttach.length;i++)
                { 

                    var xhrAttach = Ti.Network.createHTTPClient();
                        xhrAttach.timeout = 15000;
                        xhrAttach.onsendstream = function(e){


                        };

                        xhrAttach.onreadystatechange = function() {

                            if (xhrAttach.readyState != 4) return;

                                    if ((i == theAttach.length) && (xhrAttach.readyState == 4))
                                    {               

                                    }           

                        };

                        xhrAttach.onerror = function() {
                        };


                            xhrAttach.open('POST', url, true);
                            xhrAttach.setRequestHeader('User-Agent', theuseragent());           
                            xhrAttach.send(AttachmentTransmitArray);        

                }                           
Était-ce utile?

La solution

Cocco nailed it! He suggested I cache each xhr into a container array, therefore I could access the individual xhr and abort it that way ie xhrAttach[i].abort()

I did this and it works perfectly! Thanks cocco!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top