Firefox addon 在firefox sdk builder中下载文件(代码段)

Firefox addon 在firefox sdk builder中下载文件(代码段),firefox-addon,firefox-addon-sdk,Firefox Addon,Firefox Addon Sdk,我试图在mozila sdk builder中获得第一个块代码。web IDE 这是mozilla SDK builder的代码 由于这个错误 发生异常。 未定义:XPConnect undefined 6中出现意外错误如果查看错误控制台,您很可能会看到服务未定义 添加以下行 Components.utils.impotresource://gre/modules/Services.jsm; 如果您查看错误控制台,您很可能会看到服务未定义 添加以下行 Components.utils.impo

我试图在mozila sdk builder中获得第一个块代码。web IDE

这是mozilla SDK builder的代码

由于这个错误

发生异常。
未定义:XPConnect undefined 6中出现意外错误如果查看错误控制台,您很可能会看到服务未定义

添加以下行

Components.utils.impotresource://gre/modules/Services.jsm;
如果您查看错误控制台,您很可能会看到服务未定义

添加以下行

Components.utils.impotresource://gre/modules/Services.jsm; 这应该行得通。但第一个问题仍然是个秘密

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");
这应该行得通。但第一个问题仍然是个秘密

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中,文件将在后台下载,无需弹出窗口或下载管理器

//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);

将其复制/粘贴到main.js中,文件将在后台下载,无需弹出窗口或下载管理器

在添加导入服务模块的行之后,这是原始错误还是新错误?在我向您指出必须进行的更改之前,请务必明确您的代码导入服务模块。您添加了该行吗?这是原始错误还是添加了导入服务模块的行后出现的新错误?在我向您指出必须进行的更改之前,请务必明确您的代码导入了服务模块。你加上那一行了吗?
//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);