Electron-dailog选择文件

进程:主进程

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

进程:渲染进程

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

结果:取消

{
  canceled: true
  filePaths: Array(0)
}

结果:成功

{
  canceled: false
  filePaths: ["E:dirnamefileName1.md","E:dirname2fileName2.md"]
}

dialog.showOpenDialog([browserWindow, ]options)

  • browserWindow BrowserWindow (可选)
  • 选项 对象
    • title String (可选) - 对话框窗口的标题
    • defaultPath String (可选) - 对话框的默认展示路径
    • buttonLabel String (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。
    • filters FileFilter[] (可选)
    • properties String-包括对话框应当使用的特性。 支持以下属性值:
      • openFile - 允许选择文件
      • openDirectory - 允许选择文件夹
      • multiSelections-允许多选。
      • showHiddenFiles-显示对话框中的隐藏文件。
      • createDirectory macOS -允许你通过对话框的形式创建新的目录。
      • promptToCreate Windows-如果输入的文件路径在对话框中不存在, 则提示创建。 这并不是真的在路径上创建一个文件,而是允许返回一些不存在的地址交由应用程序去创建。
      • noResolveAliases macOS-禁用自动的别名路径(符号链接) 解析。 所选别名现在将会返回别名路径而非其目标路径。
      • treatPackageAsDirectory macOS -将包 (如 .app  文件夹) 视为目录而不是文件。
      • dontAddToRecent Windows - Do not add the item being opened to the recent documents list.
    • message String (可选) macOS -显示在输入框上方的消息。
    • 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[] - 用户选择的文件路径的数组. 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 参数允许该对话框将自身附加到父窗口, 作为父窗口的模态框。

原文地址:https://www.cnblogs.com/baixinL/p/14222884.html