【问题标题】:How to select file on fs with react and electron?如何使用反应和电子在 fs 上选择文件?
【发布时间】:2018-06-27 09:46:44
【问题描述】:

我正在使用 Electron 和 ReactJS 构建桌面应用程序。
我需要实现从文件系统中选择文件的功能,例如 input="file" 在表单中工作。
实际上,我只需要获取文件的绝对路径即可。
我怎样才能做到这一点?

我试过了:

<input type="file" onChange={function(e) {console.log(e.target.value)} } />

但出于安全原因,它会返回 fakepath。
我认为 Electron 中的对话框可能对此有用,但是如何传播文件路径以响应应用程序呢?

【问题讨论】:

  • 可以this帮助你吗?
  • 我需要以用户友好的方式获取文件的路径。
  • 定义“用户友好”
  • 当您在网上上传一些文件时,会出现文件选择对话框。我需要类似的东西,但目标是获取所选文件的绝对路径。
  • 你试过使用showOpenDialog吗?

标签: javascript reactjs electron


【解决方案1】:
    const {dialog} = require('electron').remote;

...

document.querySelector('#fileSelect').addEventListener('click', function (event) {
    dialog.showOpenDialog({
        properties: ['openFile', 'multiSelections']
    }, function (files) {
        if (files !== undefined) {
            // handle files
        }            
    })
});

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 2021-01-03
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多