Here's a stripped down version of a function I've used to download a zip
file. saveURI is asynchronous, so if you need to do anything when the
download is complete, set the progressListener.

function download() {
  const FileFactory = new
Components.Constructor("@mozilla.org/file/local;1",
                                                 "nsILocalFile",
                                                 "initWithPath");
  const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
  
  try {
    // set up target destination for download
    var dest = new FileFactory("C:/some/directory");
    dest.append("myapp.zip");
    if (!dest.exists()) {
      dest.create(dest.NORMAL_FILE_TYPE, 0777);
    }
    
    // set up source
    var uri = Components.classes['@mozilla.org/network/standard-url;1']
                   .createInstance(Components.interfaces.nsIURI);
    uri.spec = "http://some/where/myapp.zip";;
    
    var persist =
Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
 
.createInstance(Components.interfaces.nsIWebBrowserPersist);
    var flags = nsIWBP.PERSIST_FLAGS_NO_CONVERSION |
                nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
                nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
    persist.persistFlags = flags;
    persist.progressListener = new PersistProgressListener;
    persist.saveURI(uri, null, null, null, null, dest);
    
  } catch(e) { 
    throw("Download failed.");
  }
}
function PersistProgressListener() {}
PersistProgressListener.prototype = {
  QueryInterface : function(aIID) {
    if(aIID.equals(Components.interfaces.nsIWebProgressListener))
      return this;
    throw Components.results.NS_NOINTERFACE;
  },

  init : function() {},
  destroy : function() {},

  onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
      if (aStateFlags &
Components.interfaces.nsIWebProgressListener.STATE_STOP) {
          // HERE DO WHATEVER AFTER THE FILE IS DOWNLOADED
      }
  },
  
  onProgressChange : function (aWebProgress, aRequest,
                               aCurSelfProgress, aMaxSelfProgress,
                               aCurTotalProgress, aMaxTotalProgress) {},
  onLocationChange : function(aWebProgress, aRequest, aLocation) {},
  onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage) {},
  onSecurityChange : function(aWebProgress, aRequest, aState) {}
}

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Marek Lewczuk
Sent: Wednesday, May 19, 2004 5:25 PM
To: [EMAIL PROTECTED]
Subject: download binary file


Does anyone has working example how to download binary files over http 
and save on local disc ?

Thanks
_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom



The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 

_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to