如何允许用户选择电子目录?

问题描述

我正在为使用我的Python脚本之一创建前端。该脚本基本上会获取一个视频文件夹,并从该文件夹中的视频制作gif。因为用户系统上存储要转换为gif的视频的文件夹将发生变化,所以我需要用户能够选择他们正在使用的目录,然后选择要保存gif的第二个目录。

到目前为止,我已打开对话框,我可以选择目录,但它返回的是该目录中的文件数,而不是路径。

以下是我当前设置的代码的副本:

Main.js

    const{dialog,ipcMain} = require('electron')
ipcMain.on('selectDir',async(event,arg)=>{
    const result = await dialog.showOpenDialog(mainWindow,{
        properties:['openDirectory','createDirectory'],})
    console.log('directories selected',result.filePaths)
})

renderer.js

var selectDir = document.getElementById('dirs').files[0].path;
selectDir.addEventListener('click',()=>{

    window.postMessage({
        type:'selectDir'
    })
})

preload.js:

const{ipcRender}= require('electron')

    process.once('loaded',() =>{
        window.addEventListener('message',evt=>{
            if(evt.data.type === 'selectDir'){
                ipcRender.send('selectDir')
            }
        })
    })

html:

<body>
    <div class='page_container'>
        <p>Add an The Directory Containing your videos and where you would like the gif's to be saved to and watch as your videos randomly create gifs</p>
            <input type='file' id='dirs' webkitdirectory/>
    </div>
</body>
<script src="./renderer.js"></script>

大部分代码来自jaketrent.com/post/select-directory-in-electron,我在stackoverflow上的其他帖子中发现了一些修改,其中有人说他们能够通过添加以下内容来检索目录路径.file [0]

目标是在窗口中打印出选定的目录(通常是文件),以便将该路径发送到python脚本。

如果任何人都可以指出我似乎出了错的地方,或者可以找到有关此问题的更多信息的链接,那真是棒极了。

据我所知,mainjs中的openDirectory选项应强制选择目录,但是在html上没有webkit的情况下,它会还原为标准文件选择器,因此我无法弄清为什么。

我可以发布整个main.js,但是除了上面的代码段中的内容与入门文件中的内容几乎相同。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)