follow-up to network
--- Begin Message --- fwiw, if you wanted to do this without access to xpconnect (like in web content), you could do something like this:

    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", gImageSource, true);
    xmlhttp.overrideMimeType('text/plain; charset=x-user-defined');

Results may vary. This was coded up at by Marcus Granado 2006 [http://mgran.blogspot.com]

Then in your onreadystatechange do something like:

            var data = xmlhttp.responseText;

            var bytes = [];
            for (var i=0; i<data.length; i++) {
                var c = data.charCodeAt(i);
                bytes.push((c > 255) ? c - 63232 : c);
            }

At this point, your data will be in the bytes var.

This isn't the right newsgroup, but we should have something like xmlhttp.rawData or something which contains a byte array of the actual returned content. I am sure that has been debated in other places.

Doug


Michel Gutierrez wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


I found the solution to my problem using binaryinputstream and
binaryoutputstream components.


                                var channel=request.channel;
                                var stream=channel.open();
                                var fos =
Components.classes["@mozilla.org/network/file-output-stream;1"].
                            
createInstance(Components.interfaces.nsIFileOutputStream);
                                var 
length=request.getResponseHeader("content-length");
                                fos.init(file,0x02 | 0x08 | 0x20, 0644, 0);
                                var bistream = 
Components.classes["@mozilla.org/binaryinputstream;1"].
                        
createInstance(Components.interfaces.nsIBinaryInputStream);
                                bistream.setInputStream(stream);
                                var bostream = 
Components.classes["@mozilla.org/binaryoutputstream;1"].
                        
createInstance(Components.interfaces.nsIBinaryOutputStream);
                                bostream.setOutputStream(fos);
                                var n=0;
                                length=parseInt(length);
                                while(n<length) {
                                        var 
ba=bistream.readByteArray(bistream.available());
                                        bostream.writeByteArray(ba,ba.length);
                                        n+=ba.length;
                                }                                       
                                fos.flush();
                                fos.close();
                                stream.close();


Michel Gutierrez wrote:
hi,

in an extension implemented in javascript, i would like to use the
nsIXmlHttpRequest object to query data from an url. based on the
returned content-type, i would like to either parse the returned file
and do some stuff, or save the binary data into a file.

i cannot find a way to save the binary data. the
@mozilla.org/network/file-output-stream;1 component seems not to
implement writeFrom method and i'm running out of ideas.

does anybody have some trick for doing so ?

thanks,
/mig


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFeCPmtMkIv0/ruZgRAoCDAKCNCbAA+RSUEIQ6OCGMMO3oGHdDpACeLFut
EZy/9QU/5OqvFNpmfKHS4lI=
=/aSh
-----END PGP SIGNATURE-----

--- End Message ---
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network

Reply via email to