如何在Firefox-Addon-sdk中使用mkdir?

时间:2013-08-04 11:44:56

标签: firefox-addon firefox-addon-sdk

如何以编程方式创建新目录/文件夹?我正在下载mp3并保存它们但我希望按类型(getDirectoryName)分隔它们。到目前为止,这是我的代码..

function DownloadFile(sLocalFileName, newDirectoryName, sRemoteFileName)
{


    var saveToDirectory = 'C:\\Users\\louis\\downloads\\'+newDirectoryName+'\\';

    console.log("downloading : "+saveToDirectory);

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


    console.log("should be downloading!!!!");

}
DownloadFile('filename.mp3','genre','http://domain.com/file.mp3');

2 个答案:

答案 0 :(得分:2)

导入OS.File module并使用makeDir

答案 1 :(得分:1)

此外,SDK提供file utilites,如mkpath(mkdir -p)

在FF25中我相信(目前在夜间),io/fs模块将可用,与node's fs module

平分