const { ipcMain } = require('electron')
// Listen for a message from the renderer process
ipcMain.on('load-local-image', (event, arg) => {
// Read the contents of the local file using the fs module
const fs = require('fs')
const imageData = fs.readFileSync('/path/to/local/image.jpg')
// Send the image data back to the renderer process
event.sender.send('local-image-loaded', imageData)
})
const { ipcRenderer } = require('electron')
// Send a message to the main process to load the local image
ipcRenderer.send('load-local-image')
// Listen for a response
ipcRenderer.on('local-image-loaded', (event, imageData) => {
// set the background image
// document.body.style.backgroundImage = `url(${imageData})` with url
document.body.style.backgroundImage = `${imageData}`
})
1条答案
按热度按时间3df52oht1#
您可以使用
fs
模块和window.postMessage
函数在文档中阅读有关
preload.js
的更多信息:https://www.electronjs.org/docs/api/web-preferences#preload。