Retrieving binary data with XMLHttpRequest, again

A few months ago I concluded that it is simply not possible to download binary files for Ajax purposes, at least not with Firefox (see Retrieving binary data with XMLHttpRequest).

Last Monday, Marcus Granado posted “Downloading Binary Streams with Javascript XMLHttpRequest“, where he demonstrates that it is possible to get a binary clean stream through XMLHtttpRequest.
It boils down to using the right Charset in with the proprietary overrideMimeType() function. Read his post to see how.

In one word: Wow! This opens up a whole lot of new applications, for instance with Greasemonkey scripts.

I’m glad I have been proven wrong!

Update: this does not work for me as is…

What happens is that all bytes above 127 (decimal) are returned as a really big value. The solution, however, appears to be very simple:

var filestream = load_url(url);
var c = filestream.charCodeAt(x);
if (c > 255) c -= 63232;

Apparently, the extra high value is not caused by extra bytes being gobbled up, but constructed on the fly. Luckily this is done in such a deterministic way.

I’m working with javascript in a UTF-8 page context and, although I have experimented with some other character sets for the page, this made no difference. I’m using Firefox 1.5.0.6, Mac OSX right now.

I’m really interested in other users’ experiences. Please share them via the feedback form!