I'd suggest moving away from the XMLHttpRequest and implementing your own nsIChannel and nsIDownloader instead, it's more flexible, allows easy access to progress info and is much simpler than at first appears:


var nsIFile = nsIFileProtocolHandler.getFileFromURLSpec("file://path/");

var nsIDownloadObserver = {
  onDownloadComplete: function(nsIDownloader, nsresult, nsIFile) {
    dump( "\nDownloadComplete" );
  }
};

var nsIDownloader = Components.classes["@mozilla.org/network/downloader;1"].createInstance();
nsIDownloader.QueryInterface(Components.interfaces.nsIDownloader);
nsIDownloader.init( nsIDownloadObserver, nsIFile );



nsIHttpChannel = nsIIOService.newChannel("http://host.etc";, "", null); nsIHttpChannel.QueryInterface(Components.interfaces.nsIHttpChannel); nsIHttpChannel.requestMethod = "GET"; // Optional as is default nsIHttpChannel.asyncOpen( nsIDownloader, nsIFile );

Job done!

Your nsIDownloadObserver will now receive the callback on onDownloadComplete(). If you want to get slicker you can also add an nsIProgressEventSink into the nsIChannel.notificationCallbacks property.

XUL Planet as ever is your bible.


N



Rogers wrote:
I am writing a Firefox extension, as a part of which i need to download and
save an image file.

I've used the responseText property of XMLHttpRequest and file-output-stream
to download and save simple HTML files. But this technique does not seem to
work with jpeg images. I also tried finding something analogous to the
responseBody property provided by MSCOM, but in vain.

Adding these two lines also doesn't help.
       httpRequest.setRequestHeader ("Content-type", "image/jpeg");
        httpRequest.overrideMimeType('image/jpeg');

what is the difference in code to download and save a plaintext/html file vs
a Jpeg image?

I've been struggling with this for a long time now and would really
appreciate some help on this.

Thanks!


_______________________________________________
Mozilla-xpcom mailing list
Mozilla-xpcom@mozilla.org
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to