日期:2014-05-16  浏览次数:20323 次

JavaScript实现文件下载
function downloadFile(url)
{
  var file = 
    Components.classes["@mozilla.org/file/local;1"] 
      .createInstance(Components.interfaces.nsILocalFile); 

  file.initWithPath("C:\\example.txt");

  var ios = Components.classes['@mozilla.org/network/io-service;1']
              .getService(Components.interfaces.nsIIOService);
  var uri = ios.newURI(url, null, null);

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

  const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
  const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;

  persist.saveURI(uri, null, null, null, null, file);

}