When I set uriObj.spec to an html page url, the following Javascript
code executes correctly. However, if I set the uriObj.spec to the url
of a jpeg image (which displays in a browser and which I can access with
WinInet), this code does not work -- onDataAvailable gets control once.
The "count" is correct -- it contains the number of bytes in the image,
but when I do the read, it returns only 4 bytes. Does anyone know how
the code needs to be modified to access binary files?
function getImageFromURL(inputURLString){
var fileContents;
//Read image from inputURLString into fileContents;
try {
const IOService =
Components.classes['@mozilla.org/network/io-service;1']
.getService(Components.interfaces.nsIIOService);
var uriObj =
Components.classes['@mozilla.org/network/standard-url;1']
.createInstance(Components.interfaces.nsIURI);
uriObj.spec = "http://www.google.com";
uriObj.spec = inputURLString;
var channel = IOService.newChannelFromURI(uriObj);
var downloadListener = {
data : "",
onStartRequest: function(request, context) {},
onStopRequest: function(request, context, status) {
alert("in downloadListener, at onStop, data is: " +
this.data);
listener.finished(this.data, this);
},
onDataAvailable: function(request, context, inputStream, offset,
count) {
alert("reading data. count is: " + count);
var scriptableStream = toScriptableStream(inputStream);
var mydata = scriptableStream.read(count);
this.data = this.data + mydata;
alert("read data, mydata.length = " + mydata.length);
},
};
channel.asyncOpen(downloadListener, null);
}
catch(e) {
alert("In getImageFromURL, error reading image = " + e);
}
function toScriptableStream(input) {
var SIStream =
Components.Constructor('@mozilla.org/scriptableinputstream;1',
'nsIScriptableInputStream', 'init');
return new SIStream(input);
}
_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom