Question

Iam building an extension and i want to take the path of a file from a window popup like windows does . Is something like that to use in an extension in firefox?

enter image description here

Was it helpful?

Solution

You should check out this page on MDN

Creating a file picker

To begin, you need to create a file picker component and initialize it.

var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Select a File", nsIFilePicker.modeOpen);

First, a new file picker object is created and stored in the variable 'fp'. The 'init' function is used to initialize the file picker. This function takes three arguments, the window that is opening the dialog, the title of the dialog and the mode. The mode here is modeOpen which is used for an Open dialog. You can also use modeGetFolder and modeSave for the other two modes. These modes are constants of the nsIFilePicker interface.

Getting the selected file

Finally, you can show the dialog by calling the show() function. It takes no arguments but returns a status code that indicates what the user selected. Note that the function does not return until the user has selected a file. The function returns one of three constants:

returnOK - the user selected a file and pressed OK. The file the user selected will be stored in the file picker's file property.

returnCancel - the user pressed Cancel.

returnReplace - in the save mode, this return value identifies that the user selected a file to be replaced. (returnOK will be returned when the user entered the name of a new file.) You should check the return value and then get the file object from the file picker using the file property.

var res = fp.show();
if (res != nsIFilePicker.returnCancel){
  var thefile = fp.file;
  // --- do something with the file here ---
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top