httprequest and binary data in javascript

1.1k Views Asked by At

Still struggling after lots of tries to read the response of a httprequest that is a binary datastream which will represent an jpg image.

edit: the whole thing

xmlhttp = false;
        /*@cc_on@*/
        /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
        @end@*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
                xmlhttp = new XMLHttpRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && window.createRequest) {
            try {
                xmlhttp = window.createRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }

        xmlhttp.open("GET", theUrl, true);  
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {

                var headers = xmlhttp.getAllResponseHeaders(); 
            }
        }

        xmlhttp.send(null);

I am using IE8, and no HTML 5 (also tried in FF12) so i always end up with errors with something like

xhr.overrideMimeType('text\/plain; charset=x-user-defined');

or

xhr.responseType = "arraybuffer";

even copying into a variable won´t work

var body = xhr.response; //.responseText , .responseBody

any ideas whats wrong or wwhat i could try?

1

There are 1 best solutions below

1
On

I made this example to read binary JPEG on client-side and parse EXIF data. It uses BinFileReader.js which makes it really easy to deal with binary data, cross-browser. Here is the whole thing in a zip file, including a node-based webserver (run with node server.js) I included a web-worker example, also.

If you are just making an image out of binary stream, why not just add <img src="blah" /> to your DOM?

$('body').append('<img src="' + URL + '"/>');