Electron支持多个文件选中功能(showOpenDialog)

如果你使用Electron作为客户端开发框架的话,那么有可能会遇到打开本地目录,并选中多个文件的需求。

如果遇到这样的问题,改怎么解决呢?

先来看张图:

这就是运行效果, 具体的代码如下:

// 导入对话框API模块
const {
    BrowserWindow,
    app,
    dialog
} = require('electron');

// 打开系统目录
dialog.showOpenDialog(mainWindow, {
    properties: ['openFile','multiSelections'],
    // 支持文件格式筛选,同时也支持自定义格式
    filters: [
        { name: 'Images', extensions: ['jpg', 'png', 'gif'] },
        { name: 'All Files', extensions: ['*'] }
      ]
}).then(result => {
    // 打印选中状态
    console.log(result.canceled);
    // 打印选中文件数组(绝对路径)
    console.log(result.filePaths);
    // todo 其他操作
}).catch(err => {
    // 捕获异常
    console.log(err);
})

代码可以直接复制粘贴使用,本人已经在工程中实测。

猜你喜欢

转载自blog.csdn.net/liuzehn/article/details/106681313