NsIFilePicker: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(moving nsIWindowMediator info to a separate article, trying to keep it simple)
Line 1: Line 1:
{{extdev}}
{{wrongtitle|title=nsIFilePicker}}
{{wrongtitle|title=nsIFilePicker}}


{{extdev}}
The filepicker component is used to display standart "Open File"/"Save File"/"Select Folder" dialogs.


==Using nsIFilePicker==
== Example ==
 
Here's an example:
<pre>const nsIFilePicker = Components.interfaces.nsIFilePicker;
<pre>
const nsIFilePicker = Components.interfaces.nsIFilePicker;


var fp = Components.classes["@mozilla.org/filepicker;1"]
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
.createInstance(nsIFilePicker);
fp.init(windowHandle, "Dialog Title", nsIFilePicker.modeOpen);
fp.init(window, "Dialog Title", nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);


Line 18: Line 20:
   // work with returned nsILocalFile...
   // work with returned nsILocalFile...
}
}
</pre>


// to get a simple windowHandle you may use .. (returns the any most recent one)
If your code is a component and <code>window</code> is not defined, you can get one using [[nsIWindowMediator]].


  const kWindowMediatorContractID = "@mozilla.org/appshell/window-mediator;1";
== Notes ==
  const kWindowMediatorIID = Components.interfaces.nsIWindowMediator;
* If you pass empty string as dialog title, the dialog will have default title
  const kWindowMediator = Components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID);
* When checking return value for Save dialog, be sure to check for nsIFilePicker.returnReplace too.
  var windowHandle = kWindowMediator.getMostRecentWindow(null);
* Available modes: modeOpen, modeSave, modeGetFolder, modeOpenMultiple.
* Available filters: filterAll, filterHTML, filterText, filterImages, filterXML, filterXUL, filterApps.


</pre>
----
{{msg:stub}}


==Notes==
== References ==
*If you pass empty string as dialog title, the dialog will have default title
*When checking return value for Save dialog, be sure to check for nsIFilePicker.returnReplace too.
*Available modes: modeOpen, modeSave, modeGetFolder, modeOpenMultiple.
*Available filters: filterAll, filterHTML, filterText, filterImages, filterXML, filterXUL, filterApps.
 
==References==
*[http://xulplanet.com/references/xpcomref/ifaces/nsIFilePicker.html nsIFilePicker interface].
*[http://xulplanet.com/references/xpcomref/ifaces/nsIFilePicker.html nsIFilePicker interface].
----
{{msg:stub}}


[[Category:Example code|nsIFilePicker]] [[Category:XPCOM example code|nsIFilePicker]] [[Category:JavaScript example code|nsIFilePicker]]
[[Category:Example code|nsIFilePicker]] [[Category:XPCOM example code|nsIFilePicker]] [[Category:JavaScript example code|nsIFilePicker]]

Revision as of 23:50, 27 April 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

The filepicker component is used to display standart "Open File"/"Save File"/"Select Folder" dialogs.

Example

Here's an example:

const nsIFilePicker = Components.interfaces.nsIFilePicker;

var fp = Components.classes["@mozilla.org/filepicker;1"]
	.createInstance(nsIFilePicker);
fp.init(window, "Dialog Title", nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);

var rv = fp.show();
if (rv == nsIFilePicker.returnOK)
{
  var file = fp.file;
  // work with returned nsILocalFile...
}

If your code is a component and window is not defined, you can get one using nsIWindowMediator.

Notes

  • If you pass empty string as dialog title, the dialog will have default title
  • When checking return value for Save dialog, be sure to check for nsIFilePicker.returnReplace too.
  • Available modes: modeOpen, modeSave, modeGetFolder, modeOpenMultiple.
  • Available filters: filterAll, filterHTML, filterText, filterImages, filterXML, filterXUL, filterApps.

References