对话框

对话框

显示用于打开和保存文件、警报等的本机系统对话框。

进程:主进程

An example of showing a dialog to sel​ect multiple files:

const { dialog } = require('electron')console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

The Dialog is opened from Electron’s main thread. If you want to use the dialog object from a renderer process, remember to access it using the remote:

const { dialog } = require('electron').remoteconsole.log(dialog)

方法

dialog 模块具有以下方法:

dialog.showOpenDialogSync([browserWindow, ]options)

  • browserWindow BrowserWindow (可选)

  • options Object

  • title String (可选)

  • defaultPath String (可选)

  • buttonLabel String (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。

  • filters FileFilter[] (可选)

  • properties String[] (optional) - Contains which features the dialog should use. The following values are supported:

  • openFile - 允许选择文件

  • openDirectory - 允许选择文件夹

  • multiSelections-允许多选。

  • showHiddenFiles-显示对话框中的隐藏文件。

  • createDirectory macOS - Allow creating new directories from dialog.

  • promptToCreate Windows - Prompt for creation if the file path entered in the dialog does not exist. 这并不是真的在路径上创建一个文件,而是允许返回一些不存在的地址交由应用程序去创建。

  • noResolveAliases macOS - Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.

  • treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

  • dontAddToRecent Windows - Do not add the item being opened to the recent documents list.

  • message String (optional) macOS - Message to display above input boxes.

  • securityScopedBookmarks Boolean (optional) macOS mas - Create security scoped bookmarks when packaged for the Mac App Store.

Returns String[] | undefined, the file paths chosen by the user; if the dialog is cancelled it returns undefined.

browserWindow 参数允许该对话框将自身附加到父窗口, 作为父窗口的模态框。

The filters specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. 例如:

{  filters: [    { name: 'Images', extensions: ['jpg', 'png', 'gif'] },    { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },    { name: 'Custom File Type', extensions: ['as'] },    { name: 'All Files', extensions: ['*'] }  ]}

extensions 数组应为没有通配符或点的扩展名 (例如, "png" 是正确的, 而 ".png" 和 . png " 就是错误的)。 若要显示所有文件, 请使用 "" 通配符 (不支持其他通配符)。

Note: On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set properties to ['openFile', 'openDirectory'] on these platforms, a directory selector will be shown.

dialog.showOpenDialogSync(mainWindow, {  properties: ['openFile', 'openDirectory']})

dialog.showOpenDialog([browserWindow, ]options)

  • browserWindow BrowserWindow (可选)

  • options Object

  • title String (可选)

  • defaultPath String (可选)

  • buttonLabel String (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。

  • filters FileFilter[] (可选)

  • properties String[] (optional) - Contains which features the dialog should use. The following values are supported:

  • openFile - 允许选择文件

  • openDirectory - 允许选择文件夹

  • multiSelections-允许多选。

  • showHiddenFiles-显示对话框中的隐藏文件。

  • createDirectory macOS - Allow creating new directories from dialog.

  • promptToCreate Windows - Prompt for creation if the file path entered in the dialog does not exist. 这并不是真的在路径上创建一个文件,而是允许返回一些不存在的地址交由应用程序去创建。

  • noResolveAliases macOS - Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.

  • treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

  • dontAddToRecent Windows - Do not add the item being opened to the recent documents list.

  • message String (optional) macOS - Message to display above input boxes.

  • securityScopedBookmarks Boolean (optional) macOS mas - Create security scoped bookmarks when packaged for the Mac App Store.

Returns Promise<​Object> - Resolve with an object containing the following:

  • canceled Boolean - whether or not the dialog was canceled.

  • filePaths String[] - An array of file paths chosen by the user. If the dialog is cancelled this will be an empty array.

  • bookmarks String[] (optional) macOS mas - An array matching the filePaths array of base64 encoded strings which contains security scoped bookmark data. securityScopedBookmarks 必须启用才能捕获数据。 (For return values, see table here.)

browserWindow 参数允许该对话框将自身附加到父窗口, 作为父窗口的模态框。

The filters specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. 例如:

{  filters: [    { name: 'Images', extensions: ['jpg', 'png', 'gif'] },    { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },    { name: 'Custom File Type', extensions: ['as'] },    { name: 'All Files', extensions: ['*'] }  ]}

extensions 数组应为没有通配符或点的扩展名 (例如, "png" 是正确的, 而 ".png" 和 . png " 就是错误的)。 若要显示所有文件, 请使用 "" 通配符 (不支持其他通配符)。

Note: On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set properties to ['openFile', 'openDirectory'] on these platforms, a directory selector will be shown.

dialog.showOpenDialog(mainWindow, {  properties: ['openFile', 'openDirectory']}).then(result => {  console.log(result.canceled)  console.log(result.filePaths)}).catch(err => {  console.log(err)})

dialog.showSaveDialogSync([browserWindow, ]options)

  • browserWindow BrowserWindow (可选)

  • options Object

  • title String (可选)

  • defaultPath String (可选) - 默认情况下使用的绝对目录路径、绝对文件路径或文件名。

  • buttonLabel String (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。

  • filters FileFilter[] (可选)

  • message String (optional) macOS - Message to display above text fields.

  • nameFieldLabel String (optional) macOS - Custom label for the text displayed in front of the filename text field.

  • showsTagField Boolean (optional) macOS - Show the tags input box, defaults to true.

  • properties String[] (optional)

  • showHiddenFiles-显示对话框中的隐藏文件。

  • createDirectory macOS - Allow creating new directories from dialog.

  • treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

  • showOverwriteConfirmation Linux - Sets whether the user will be presented a confirmation dialog if the user types a file name that already exists.

  • dontAddToRecent Windows - Do not add the item being saved to the recent documents list.

  • securityScopedBookmarks Boolean (optional) macOS mas - Create a security scoped bookmark when packaged for the Mac App Store. 当该选项被启用且文件尚不存在时,那么在选定的路径下将创建一个空文件。

Returns String | undefined, the path of the file chosen by the user; if the dialog is cancelled it returns undefined.

browserWindow 参数允许该对话框将自身附加到父窗口, 作为父窗口的模态框。

filters 可以指定可显示文件的数组类型,详见 dialog.showOpenDialog 事例

dialog.showSaveDialog([browserWindow, ]options)

  • browserWindow BrowserWindow (可选)

  • options Object

  • title String (可选)

  • defaultPath String (可选) - 默认情况下使用的绝对目录路径、绝对文件路径或文件名。

  • buttonLabel String (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。

  • filters FileFilter[] (可选)

  • message String (optional) macOS - Message to display above text fields.

  • nameFieldLabel String (optional) macOS - Custom label for the text displayed in front of the filename text field.

  • showsTagField Boolean (optional) macOS - Show the tags input box, defaults to true.

  • properties String[] (optional)

  • showHiddenFiles-显示对话框中的隐藏文件。

  • createDirectory macOS - Allow creating new directories from dialog.

  • treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

  • showOverwriteConfirmation Linux - Sets whether the user will be presented a confirmation dialog if the user types a file name that already exists.

  • dontAddToRecent Windows - Do not add the item being saved to the recent documents list.

  • securityScopedBookmarks Boolean (optional) macOS mas - Create a security scoped bookmark when packaged for the Mac App Store. 当该选项被启用且文件尚不存在时,那么在选定的路径下将创建一个空文件。