Best way to confirm xhr download completion
I'm downloading a file via xhr. However sometimes my process will hang even though the download has actually completed. What's the best way to confirm this?
I've tried e.progress == 1 under the ondatastream function. I've also tried readyState == 4 under the onreadystatechange function. Neither is producing consistent results.
5 Answers
-
I've hit this too, and I was actually going to build a reproducible test case for a bug report today.
I think it's a bug in the iPhone implementation, since a quick test doesn't seem to trigger it on Android. If I put a button on my app that triggers an xhr request and an alert in the onload function, then loop through download/ok/download/ok a few times, the onload fails to trigger. If I start another download, I find two alerts pop up at once, so I guess it's a timing problem or race condition. I wonder if we can work around it with a timer function?
-
Good news - I was able to work around it with setInterval(). There's no need to do anything in the interval routine - the mere act of triggering the timer seems to cause the onload signal to be reliably delivered (though at a delay relative to the timer interval)
-
Where do you trigger it? In which function. Can you post an example. Cheers
-
All you need to do is call setInterval() before you run your XHR code. It doesn't matter what the called function does.
-
The setInterval appears to stop my xhr request from sending at all. I think there's a pattern with CAMERA + XHR. But it's pretty hard to pinpoint, most of the time if I use the Gallery images to send over XHR, everything is fine. Use the camera + xhr and the first time is fine, the second time the xhr doesn't respond but the request was successful, and the 3rd time - the whole damn screen locks up.