我想让我的用户通过显示对话框来选择一个文件夹。
这可能来自Firefox插件中的JavaScript吗?
答案 0 :(得分:4)
是的,有一种简单的方法可以做到这一点。通常的方法是使用nsIFilePicker。
与该页面上示例的主要区别在于,在传递给init()
方法的参数中,您将nsIFilePicker.modeGetFolder
指定为mode
。此外,鉴于您要查找目录,您只想包含nsIFilePicker.filterAll
过滤器,而不是特定扩展类型的过滤器。
来自MDN页面的示例代码,已修改用于选择文件夹(以及给定的描述性变量名称):
if (window === null || typeof window !== "object") {
//If you do not already have a window reference, you need to obtain one:
// Add a "/" to un-comment the version appropriate for your environment.
/* Add-on SDK environment:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
/* Overlay and bootstrap environments (from almost any context/scope):
var window = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var filePicker = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
filePicker.init(window, "Dialog Title", nsIFilePicker.modeGetFolder);
filePicker.appendFilters(nsIFilePicker.filterAll );
var pickerStatus = filePicker.show();
if (pickerStatus == nsIFilePicker.returnOK
|| pickerStatus == nsIFilePicker.returnReplace
) {
var file = filePicker.file;
// Get the path as string. Note that you usually won't
// need to work with the string paths.
var path = filePicker.file.path;
// work with returned nsILocalFile...
}