第7章:Electron文件系统操作(2)

7.2 文件对话框

Electron 提供了 dialog 模块用于显示文件打开和保存对话框。

7.2.1 显示文件打开对话框

主进程代码

javascript 复制代码
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');

let mainWindow;

const createMainWindow = () => {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      contextIsolation: true,
      nodeIntegration: false
    }
  });

  mainWindow.loadFile('index.html');
  mainWindow.webContents.openDevTools();
  mainWindow.on('closed', () => {
    mainWindow = null;
  });
};

app.on('ready', createMainWindow);
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('activate', () => {
  if (mainWindow === null) {
    createMainWindow();
  }
});

// 处理文件打开对话框请求
ipcMain.handle('open-file-dialog', async () => {
  const result = await dialog.showOpenDialog({
    properties: ['openFile']
  });
  if (result.canceled) {
    return { success: false };
  } else {
    return { success: true, filePaths: result.filePaths };
  }
});

预加载脚本(preload.js)

javascript 复制代码
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  openFileDialog: () => ipcRenderer.invoke('open-file-dialog')
});

渲染进程代码

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>File Dialog Example</title>
</head>
<body>
  <h1>Open File Dialog Example</h1>
  <button id="openFileDialog">Open File</button>
  <p id="filePath"></p>

  <script>
    document.getElementById('openFileDialog').addEventListener('click', async () => {
      const result = await window.electronAPI.openFileDialog();
      if (result.success) {
        document.getElementById('filePath').innerText = 'Selected file: ' + result.filePaths[0];
      } else {
        document.getElementById('filePath').innerText = 'No file selected';
      }
    });
  </script>
</body>
</html>

7.2.2 显示文件保存对话框

主进程代码

javascript 复制代码
const { app, BrowserWindow, dialog } = require("electron");
const { ipcMain } = require("electron");

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: true,
      preload: __dirname + "/preload.js",
    },
  });

  mainWindow.loadFile("index.html");
}

app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });

  // 添加ipcMain.handle处理来自渲染进程的save-file请求
  ipcMain.handle("save-file", async (event, options) => {
    try {
      const result = await dialog.showSaveDialog(mainWindow, options);
      return result.filePath; // 直接返回文件路径给renderer,无需额外的reply
    } catch (err) {
      console.error("Error showing save dialog:", err);
      throw err;
    }
  });
});

app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});

预加载脚本(preload.js)

javascript 复制代码
const { contextBridge, ipcRenderer } = require("electron");

// 在preload中暴露安全的API给渲染进程
contextBridge.exposeInMainWorld("api", {
  saveFile: (options) => {
    return ipcRenderer.invoke("save-file", options);
  },
});

渲染进程代码

javascript 复制代码
document.getElementById("saveButton").addEventListener("click", async () => {
  const options = {
    title: "Save your File",
    defaultPath: "untitled.txt",
    filters: [
      { name: "Text Files", extensions: ["txt"] },
      { name: "All Files", extensions: ["*"] },
    ],
  };

  try {
    const filePath = await window.api.saveFile(options);
    if (filePath) {
      console.log(`File will be saved at: ${filePath}`);
      // 这里可以添加保存文件的具体逻辑
    } else {
      console.log("User canceled the save operation.");
    }
  } catch (error) {
    console.error("Error during file save operation:", error);
  }
});

HTML代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Save File Example</title>
  </head>
  <body>
    <h1>Click to Save a File</h1>
    <button id="saveButton">Save File</button>

    <script src="./renderer.js"></script>
  </body>
</html>
相关推荐
dly_blog1 小时前
Vue 响应式陷阱与解决方案(第19节)
前端·javascript·vue.js
消失的旧时光-19431 小时前
401 自动刷新 Token 的完整架构设计(Dio 实战版)
开发语言·前端·javascript
console.log('npc')1 小时前
Table,vue3在父组件调用子组件columns列的方法展示弹窗文件预览效果
前端·javascript·vue.js
用户47949283569151 小时前
React Hooks 的“天条”:为啥绝对不能写在 if 语句里?
前端·react.js
我命由我123452 小时前
SVG - SVG 引入(SVG 概述、SVG 基本使用、SVG 使用 CSS、SVG 使用 JavaScript、SVG 实例实操)
开发语言·前端·javascript·css·学习·ecmascript·学习方法
用户47949283569152 小时前
给客户做私有化部署,我是如何优雅搞定 NPM 依赖管理的?
前端·后端·程序员
C_心欲无痕2 小时前
vue3 - markRaw标记为非响应式对象
前端·javascript·vue.js
qingyun9893 小时前
深度优先遍历:JavaScript递归查找树形数据结构中的节点标签
前端·javascript·数据结构
胡楚昊3 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
熬夜敲代码的小N3 小时前
Vue (Official)重磅更新!Vue Language Tools 3.2功能一览!
前端·javascript·vue.js