简体   繁体   English

双击打开文件 electron

[英]Opening files with electron from double click

yes kind of a duplicate but that code no longer works with my case.是的,有点重复,但该代码不再适用于我的情况。

So I'm building a photo viewer, and I want to be able to view files when an image file has been set to open as default with my program to open it, but I also have made custom window controls, it has images of course, addition to that the script listed at Opening files with electron messes up those images by reading them as the data sent from main.js and it messes things up所以我正在构建一个照片查看器,当图像文件设置为默认打开时,我希望能够查看文件,但我也制作了自定义 window 控件,它当然有图像,除此之外, 使用 electron 打开文件中列出的脚本通过将这些图像读取为从main.js发送的数据来搞乱这些图像,并且搞砸了

An image of how it looks when the app loads应用加载时的外观图片在此处输入图像描述

if anyone could find out a work around that'll fix this it would be very appreciated, thanks.如果有人能找到解决此问题的解决方法,将不胜感激,谢谢。

I figured it out, and it took me forever to understand the documentation and what I was doing wrong!我想通了,我花了很长时间才理解文档以及我做错了什么!

the main problem was placement主要问题是安置

main.js

this script actually would go in the function that creates the window instead of outside, so like:这个脚本实际上会 go 在 function 中创建 window 而不是在外面,所以像:

    let mainWindow;
    function createWindow() {
    // creating the window and specifying fields here
    // mainWindow = new BrowserWindow({/*fields here*/});
    // then adding the ipc listener here
        ipcMain.on('get-file-data', function(event) {
            var data = null
            if (process.platform == 'win32' && process.argv.length >= 2) {
              var openFilePath = process.argv[1];
              data = openFilePath;
            }
            event.returnValue = data;
        });
    }

    // then app.on listeners down here

renderer.js

const img = document.getElementById("image id");
var data = ipcRenderer.sendSync('get-file-data')
if (data ===  null) {
    console.log("There is no file")
} else {
    console.log(data);
    img.src = data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM