17

I'm trying to stream a large amount of binary data in JavaScript, accessing the data before the download completes. In most mainstream browsers, I can use the charset=x-user-defined trick to manually get the raw byte data during the progress event.

In Internet Explorer, however, this trick doesn't work and I'm left with using the VBArray(responseBody).toArray() method instead, which is painfully slow. However, since I only need to support IE 11 and later, I should be able to make use of IE's MSStream to get the data progressively. The following code works fine on IE 11 desktop, but not on a Lumia Windows Phone 8.1 device running IE 11 mobile:

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'ms-stream';
xhr.onreadystatechange = function () {
    if (xhr.readyState === 3 && xhr.status === 200) {
        // reader is an MSStreamReader object
        reader.readAsArrayBuffer(xhr.response);
    }
};
xhr.send();

On the Windows Phone device, the readyState never goes past 1 and the status is 0, indicating an unknown error occurred even though no actual error is thrown.

Does anyone have any idea why this isn't working for me, or maybe a solution to the problem?

19
  • Hmm, from the material I've found here, that should work. Is the code which generates the URL building the correct one? Also, is the readAsArrayBuffer method pulling the IInputStreamObject out of the MSStream via msDetachStream()? That's what it shows to do, but I'm not sure if that will completely terminate the rest of the stream and just give you whatever's downloaded currently or not. Are there other methods on MSStream to check out? May 11, 2015 at 19:05
  • Oh, you're using MSStreamReader as described here. Uh... that seems to eliminate what I said earlier. Again, is your URL formed differently between Windows Phone and desktop? May 11, 2015 at 19:50
  • @shotgun the page and the data is exactly the same on both desktop and phone, and I've tried a small separate test on both and got the same results.
    – Andy E
    May 11, 2015 at 21:08
  • This page talks about a different way to do this, under the heading "Comet Streaming". It links here, though I can't access that link currently. It looks like that's for multipart requests, though, instead of streaming... May 11, 2015 at 21:31
  • What sort of resource are you requesting via url? Does that resource have browser-detection built in? If so, Windows 8+ and IE10/11 Mobile don't seem to play nice with browser-detection scripts, as far as I've seen from a few cursory Google searches. May 11, 2015 at 21:41

1 Answer 1

2
+500

Assuming you have tried same origin policy solutions and are fairly certain that the issue is not related to same origin policy....

I think the problem is IE is not getting to readystate 3 because it doesn't get that until the entire response is received.

A workaround for this problem is to send down a two kilobyte “prelude” at the top of the response stream—my test simply sends 2kb of spaces. After that initial block is received, the onprogress event is fired as each subsequent block is received from the network.

Also, have you tried this ...

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onreadystatechange = function () {
    if (xhr.status === 200) {
        var blob = this.response;
        reader.readAsArrayBuffer(blob);
    }
};
xhr.send();
8
  • Unfortunately, it still doesn't work for same domain requests. The semi-colon is fine at the end of the function expression (because it's an expression) and most linters pull you up for it not being there. Comet streaming is the method I'm currently using, but the only way to get the raw bytes is with the following trick: new VBArray(xhr.responseBody).toArray(). This is required for the transformation to an ArrayBuffer during each progress event and becomes slower as more data is downloaded.
    – Andy E
    May 12, 2015 at 9:47
  • @AndyE Added another thing you might try with blob type. May 13, 2015 at 4:23
  • the status property is always 0, so that doesn't work either. The more I investigate the issue, the more I'm convinced that Windows Phone 8.1 has a bug that prevents 'ms-stream' being usable as a responseType XHR value.
    – Andy E
    May 13, 2015 at 8:28
  • Right , so if you change it to blob, you might have more luck. Also, test this on Desktop Win 8.1 under IE, you might find it also has problems there. May 13, 2015 at 8:29
  • When I have winphone 81 issues, i almost always see them in desktop win81 as well, so if you are not seeing them on desktop win81, then you may be right about the winphone bug May 13, 2015 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.