Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron - 正確使用 fs dialog #72

Open
NaClYen opened this issue Jul 8, 2021 · 0 comments
Open

Electron - 正確使用 fs dialog #72

NaClYen opened this issue Jul 8, 2021 · 0 comments

Comments

@NaClYen
Copy link
Owner

NaClYen commented Jul 8, 2021

前情提要

剛接觸 Electron 的時候最首要的就是要能和 local files 互動(), 但都找不到很全面說明...
當時僅拼湊出一個可用的, 但顯然不安全的作法.
因為是對內使用的專案, 安全性問題就先放一邊, 解決問題先~XD

最近在轉寫成 react 版本, 因為底層模板整個不同, 重新 survey 之下終於看到一個相對完整的討論串&說明

錯誤訊息

Uncaught TypeError: Cannot read property 'dialog' of undefined

實際案例 - easy way

base on electron-react-boilerplate

  • 修改 .\src\main.dev.ts
    new BrowserWindow() 的參數中加入啟動 remote 模組
      mainWindow = new BrowserWindow({
        show: false,
        width: 1024,
        height: 728,
        icon: getAssetPath('icon.png'),
        webPreferences: {
          enableRemoteModule: true, // <<<<< here
          nodeIntegration: true,
        },
      });
    
  • 使用方式
    import React from 'react';
    import { remote } from 'electron'; // <<<<< import here
    
    function Test9() {
      const onClick = () => {
        remote.dialog // <<<<< use as usual
          .showOpenDialog({
            title: 'open file',
            buttonLabel: 'Load',
          })
          .then((result) => {
            console.log(`file loaded: ${JSON.stringify(result)}`);
            return true;
          })
          .catch((e) => console.error(e));
      };
      return (
        <div>
          <button type="button" onClick={onClick}>
            open
          </button>
        </div>
      );
    }
    
    export default Test9;
    

(嚴謹案例之後補上XD)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant