简体   繁体   English

Electron:使用 showOpenDialog 允许在 Linux 和 Win10 上选择文件,但为什么我不能在 MacOS 上选择文件?

[英]Electron : using showOpenDialog allows files to be chosen on Linux & Win10 but why can't I choose file on MacOS?

I'm using the following code (with great success on Linux and Win10) to select a file (and ultimately read its contents).我正在使用以下代码(在 Linux 和 Win10 上取得了巨大成功)到 select 一个文件(并最终读取其内容)。

This is in my main.js and pops up a dialog on Linux and Win10 and allows me to choose a file.这是在我的 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)
  })
})

The code pops up the file open dialog on MacOS too, however, on my Mac Mini running MacOS Mojave v10.14.6 I see the following:该代码也会在 MacOS 上弹出文件打开对话框,但是,在运行 MacOS Mojave v10.14.6 的 Mac Mini 上,我看到以下内容:

Mac 打开文件对话框

Notice that I can select a folder but I cannot select any files (they are greyed out and disabled).请注意,我可以select 一个文件夹,但我不能 select 任何文件(它们是灰色和禁用的)。

I've examined the options at https://electronjs.org/docs/api/dialog but I don't see any additional option that needs to be set for MacOS to allow files to be selected.我已经检查了https://electronjs.org/docs/api/dialog中的选项,但我没有看到需要为 MacOS 设置任何其他选项以允许选择文件。 Do you know why this is occurring?你知道为什么会这样吗?

Note : As I was writing this up, I noticed something with the code that ended up being the solution.注意:当我写这篇文章时,我注意到代码中的一些东西最终成为了解决方案。 However, since SO says you can post an answer a question that hasn't been asked and because this is an interesting problem I decided to post and answer.但是,由于 SO 说您可以发布一个尚未被问到的问题的答案,并且因为这是一个有趣的问题,所以我决定发布并回答。

After staring at the documentation for quite a while I noticed the problem.在盯着文档很长一段时间后,我注意到了这个问题。 It was very subtle.这是非常微妙的。 Here's the important part from the documentation:这是文档中的重要部分: 医生说……

Oops!哎呀! The properties value is openFile not openFile s .属性值是openFile而不是 openFile s

The red herring was the fact that this did work properly (with the wrong property value) on Linux and Win10.红鲱鱼的事实是,这在 Linux 和 Win10 上确实可以正常工作(使用错误的属性值)。

Here is the fixed code:这是固定代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM