2012-12-27 30 views

回答

2

因此,使用從Firefox的nsIWebBrowserPersist docs中解除的一些代碼,我將它拼湊在一起。我不明白涉及的一些範圍問題,這不會提示用戶在哪裏或如何保存。爲了我的目的,它的工作原理,但我想如果有一個更好的解決方案。

function DownloadImage(aURLToDownload, aSaveToFile) 
{ 
    try { 

     // download from: aURLToDownload 
     var downloadURI = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(aURLToDownload, null, null); 
     console.log("Saving from: " + aURLToDownload); 

     // download destination 
     console.log("Saving as: " + aSaveToFile); 
     var outputFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); 
     outputFile.initWithPath(aSaveToFile) 

     var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist); 

     persist.progressListener = { 
      // onComplete: function(){ 
       // alert("Download complete: " + aSaveToFile); 
      // } 
      onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) { 
       var percentComplete = (aCurTotalProgress/aMaxTotalProgress)*100; 
       console.log(percentComplete +"%"); 
      } 
      // onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) { 
      // } 
     }; 

     persist.saveURI(downloadURI, null, null, null, "", outputFile); 
    } catch (e) { 
     console.log(e); 
    } 
}