httprequest and binary data in javascript
Asked Answered
C

1

6

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?

Cleghorn answered 24/5, 2012 at 12:55 Comment(6)
Why? What are you trying to accomplish? Can your server do b64 encoding?Julee
no its an embedded device and the company won´t change their interface sadlyCleghorn
But then how are you supposed to use the binary data on the script side?Hyacinthhyacintha
the plan is to create the image out of the images data streamCleghorn
You might want to look at thisLocarno
@Locarno thx i already knew about this link, but it will include vbscript which i tried to avoid, but i will try the solution until something else is foundCleghorn
B
1

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 + '"/>');
Bandler answered 14/11, 2012 at 22:47 Comment(1)
Does this answer your question?Bandler

© 2022 - 2024 — McMap. All rights reserved.