Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httprequest and binary data in javascript

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?

like image 664
Gobliins Avatar asked May 24 '12 12:05

Gobliins


People also ask

What is binary data in JavaScript?

Binary data in JavaScript is implemented in a non-standard way, compared to other languages. But when we sort things out, everything becomes fairly simple. The basic binary object is ArrayBuffer – a reference to a fixed-length contiguous memory area.

Does JavaScript support binary data?

You can send JavaScript typed arrays as binary data as well. This is building a 512-byte array of 8-bit integers and sending it; you can use any binary data you'd like, of course.

Can we send binary data in JSON?

The JSON format natively doesn't support binary data. The binary data has to be escaped so that it can be placed into a string element (i.e. zero or more Unicode chars in double quotes using backslash escapes) in JSON.

What is data binary in curl?

--data-binary is a curl SPECIFIC flag for curl itself. it has nothing to do with HTTP web services call specifically, but it's how you "POST" data to the call in the HTTP BODY instead of in the header WHEN using curl.


1 Answers

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 + '"/>');
like image 83
konsumer Avatar answered Sep 30 '22 08:09

konsumer