【问题标题】:Electron : using showOpenDialog allows files to be chosen on Linux & Win10 but why can't I choose file on MacOS?Electron:使用 showOpenDialog 允许在 Linux 和 Win10 上选择文件,但为什么我不能在 MacOS 上选择文件?
【发布时间】:2020-02-26 02:15:19
【问题描述】:

我正在使用以下代码(在 Linux 和 Win10 上取得了巨大成功)来选择一个文件(并最终读取其内容)。

这是在我的 main.js 中,在 Linux 和 Win10 上会弹出一个对话框,让我选择一个文件。

ipc.on('open-file-dialog', function (event) {
  dialog.showOpenDialog({
    properties: ['openFiles'],
    defaultPath: specialFoldersPath,

  }, function (files) {
    if (files) event.sender.send('selected-file', files)
  })
})

代码也会在 MacOS 上弹出文件打开对话框,但是,在运行 MacOS Mojave v10.14.6 的 Mac Mini 上,我看到以下内容:

请注意,我可以选择一个文件夹,但我无法选择任何文件(它们呈灰色并被禁用)。

我已经检查了https://electronjs.org/docs/api/dialog 的选项,但我没有看到需要为 MacOS 设置任何其他选项以允许选择文件。你知道为什么会这样吗?

注意:在我写这篇文章的时候,我注意到代码中的一些东西最终成为了解决方案。但是,既然 SO 说您可以发布一个尚未被问到的问题的答案,并且因为这是一个有趣的问题,我决定发布并回答。

【问题讨论】:

    标签: node.js macos electron openfiledialog


    【解决方案1】:

    在盯着documentation 看了很久之后,我注意到了这个问题。 这是非常微妙的。这是文档中的重要部分:

    哎呀!属性值为 openFile 不是 openFiles

    红鲱鱼是事实,这在 Linux 和 Win10 上确实工作正常(具有错误的属性值)。

    这里是固定代码:

    ipc.on('open-file-dialog', function (event) {
      dialog.showOpenDialog({
        properties: ['openFile'],
        defaultPath: specialFoldersPath,
    
      }, function (files) {
        if (files) event.sender.send('selected-file', files)
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2022-11-18
      • 2020-03-11
      • 1970-01-01
      • 2019-12-11
      • 2023-04-01
      • 1970-01-01
      • 2018-10-26
      • 2015-05-29
      相关资源
      最近更新 更多