2

我正在尝试在 mozila sdk builder 中使用第一个块代码。(网络 IDE)

https://developer.mozilla.org/en-US/docs/Code_snippets/Downloading_Files

这是 Mozilla SDK 构建器的代码

var {Cc, components , Cu} = require("chrome");

Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

const WebBrowserPersist = components.Constructor("@mozilla.org/embedding/browser/nsWebBrowserPersist;1",
                                                 "nsIWebBrowserPersist");

var persist = WebBrowserPersist();

var targetFile = Services.dirsvc.get("Desk", Ci.nsIFile);
targetFile.append("file.bin");

// Obtain the privacy context of the browser window that the URL
// we are downloading comes from. If, and only if, the URL is not
// related to a window, null should be used instead.

persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
                     | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

persist.saveURI('https://forums.mozilla.org/addons/styles/ca_gen2/imageset/logo.png', null, null, null, "", 'c:\\temp\\', null);

有了这个错误

An exception occurred. undefined: Unexpected error in XPConnect undefined 6

4

4 回答 4

2
function DownloadFile(sLocalFileName, sRemoteFileName)
{
    var saveToDirectory = 'C:\\Users\\louis\\downloads\\';

    var chrome = require("chrome");

    var oIOService = chrome.Cc["@mozilla.org/network/io-service;1"].getService(chrome.Ci.nsIIOService)

    var oLocalFile = chrome.Cc["@mozilla.org/file/local;1"].createInstance(chrome.Ci.nsILocalFile);
    oLocalFile.initWithPath(saveToDirectory + sLocalFileName);

    var oDownloadObserver = {onDownloadComplete: function(nsIDownloader, nsresult, oFile) {console.log('download complete...')}};

    var oDownloader = chrome.Cc["@mozilla.org/network/downloader;1"].createInstance();
    oDownloader.QueryInterface(chrome.Ci.nsIDownloader);
    oDownloader.init(oDownloadObserver, oLocalFile);

    var oHttpChannel = oIOService.newChannel(sRemoteFileName, "", null);
    oHttpChannel.QueryInterface(chrome.Ci.nsIHttpChannel);
    oHttpChannel.asyncOpen(oDownloader, oLocalFile);    

}
DownloadFile("saveAsThis.mp3","http://domain.com/file.mp3");

将其复制/粘贴到您的 main.js 中,该文件将在后台下载,没有弹出窗口或下载管理器。

于 2013-08-04T04:34:23.050 回答
1

https://github.com/inbasic/iaextractor/blob/master/src/lib/download.js

这应该有效。但拳头问题仍然是个秘密。

于 2013-06-24T20:18:36.000 回答
1
//add Ci
var {Cc, Ci, Cu} = require("chrome");
//import Services
var { Services } = Cu.import("resource://gre/modules/Services.jsm");

//the 6th arg of saveURI must a nsIFile object, can't use string
var oLocalFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
oLocalFile.initWithPath("c:\\temp\\logo.png");
if(!oLocalFile.exists()){
    oLocalFile.create(oLocalFile.NORMAL_FILE_TYPE, 0666);
}

//create the persist variable like this
var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist);
persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
                 | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

//the 1st arg of saveURI must a URI object
persist.saveURI(Services.io.newURI('https://forums.mozilla.org/addons/styles/ca_gen2/imageset/logo.png', null, null), null, null, null, "", oLocalFile, null);
于 2014-03-25T03:32:40.817 回答
0

如果您查看错误控制台,您很可能会看到Services未定义。

添加以下行

Components.utils.import("resource://gre/modules/Services.jsm");
于 2013-06-18T17:50:40.123 回答