我有这个代码在我的后台.js文件的一个Electron 应用程序.

ipcMain.on('init', (event, ...args) => {
    fileSelector = dialog.showOpenDialog({
        title: 'Select template',
        buttonLabel: 'Upload file',
        filters: [
            {
                name: 'MS Word document', extensions: ['docx']
            }
        ]
    }).then( (filePath) => {
        //fs.readFile(filePath.filePaths[0], (err, data) => {
            event.sender.send('documentPath', filePath.filePaths[0])
        //})
    }).catch( (error) => console.warn(error) )
})

基本上,我打开了一个本地文件 Select 器,我将在应用程序的VUE前端发送路径

        ipcRenderer.send('init')
        ipcRenderer.receive('documentPath', (filePath) => {
            //this.store.docTemplate = new Blob(data, { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }).arrayBuffer()
            fs.readFile(filePath, (error, data) => {
                this.store.docTemplate = data
            })
            console.log(this.store.docTemplate)
        })

我在给装在vuejs挂钩上的ipcRenderer上的听众打电话.我需要做的是从收到的文件路径中读取文件并获得BLOB,如果使用<input type="file">获得所需文件,则会正常发生BLOB.我需要将斑点传递给this library,那么有什么方法可以实现这一点吗?我try 启用 node 集成,但这会给IPC进程带来问题.

谢谢你的帮助.

推荐答案

经过 comments 中的讨论和一些本地测试,很明显,easy-template-x似乎不支持浏览器中的ArrayBuffer个对象,尽管自述文件说它支持.

但是,这不是问题,因为您完全可以在主线程中处理模板.这就是无论如何都应该做这种事情的地方,这样你的渲染器就可以做更多的渲染器事情.最好是尽可能地利用第二个过程.JS只有一个线程,所以拥有两个进程真的很奢侈!

我已经修改了您的示例代码,以显示如何做到这一点,并使用async/await使所有内容更具可读性.您可能需要添加更多的判断,但它在我使用`Easy-Template-x@3.1.0时运行得很好

我还将您的send/on/receive struct 更改为使用ipcRenderer.invokeipcMain.handle的更简单的 struct .优点是主进程可以返回对调用的响应,这允许某种形式的双向通信.有关更多信息,请参见this page of the Electron docs

main.js

const { readFile, writeFile } = require("fs/promises");
const { TemplateHandler } = require("easy-template-x");

ipcMain.handle("processTemplate", async (event, args) => {
  const loadDialog = await dialog.showOpenDialog({
    title: "Select template",
    buttonLabel: "Upload file",
    filters: [
      {
        name: "MS Word document",
        extensions: ["docx"],
      },
    ],
  });

  const selected = loadDialog.filePaths[0];

  if (selected) {
    const file = await readFile(selected);
  
    const handler = new TemplateHandler();
    const doc = await handler.process(file, params);
  
    const saveDialog = await dialog.showSaveDialog({
      title: "Save document",
      buttonLabel: "Save document",
      filters: [
        {
          name: "MS Word document",
          extensions: ["docx"],
        },
      ],
    });
    
    if (saveDialog.filePath) {
      await writeFile(saveDialog.filePath, doc);
    }
  }
});

preload.js

const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld("templates", {
  processTemplate: (params) => ipcRenderer.invoke("processTemplate", params),
});

renderer.js

async function process() {
  await window.templates.processTemplate(templateParams);
}

如果关于该代码有任何不清楚的地方,或者您有更多的问题,请随时提问!

Javascript相关问答推荐

使用JavaScript在ionic Web应用程序中更新.pane和.view的背景 colored颜色

我应该在redux reducer中调用其他reducer函数吗?

如何在Javascript中的控制台上以一行形式打印循环的结果

显示图—如何在图例项上添加删除线效果?

Prisma具有至少一个值的多对多关系

使用Promise.All并发解决时,每个promise 的线性时间增加?

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

对网格项目进行垂直排序不起作用

如何使用JavaScript拆分带空格的单词

为什么我的getAsFile()方法返回空?

Eval vs函数()返回语义

连接到游戏的玩家不会在浏览器在线游戏中呈现

未加载css colored颜色 ,无法将div设置为可见和不可见

使用Document.Evaluate() Select 一个包含撇号的HTML元素

使用自动识别发出信号(&Q)

AG-GRIDreact 显示布尔值而不是复选框

在每次重新加载页面时更改指针光标

将字符串解释为数字;将其重新编码为另一个基数

MAT-TREE更多文本边框对齐问题

使用重新 Select 和对象理解 Select 器备忘