简体   繁体   中英

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).

This is in my main.js and pops up a dialog on Linux and Win10 and allows me to choose a file.

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:

Mac 打开文件对话框

Notice that I can select a folder but I cannot select any files (they are greyed out and disabled).

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. 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.

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 .

The red herring was the fact that this did work properly (with the wrong property value) on Linux and 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)
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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