First of all you need to grasp some concepts:
Action happens on the renderer
which gets the dropped files using HTML5 FIle API
and passes file paths to main
using electron's IPC
.
renderer.js
document.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
document.addEventListener('drop', (event) => {
event.preventDefault();
event.stopPropagation();
let pathArr = [];
for (const f of event.dataTransfer.files) {
// Using the path attribute to get absolute file path
console.log('File Path of dragged files: ', f.path)
pathArr.push(f.path); // assemble array for main.js
}
console.log(pathArr);
const ret = ipcRenderer.sendSync('dropped-file', pathArr);
console.log(ret);
});
main.js
let winmain;
function createWindow () {
winMain = new BrowserWindow({
width: 1280,
height: 720,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
})
winMain.loadFile('index.html');
}
ipcMain.on('dropped-file', (event, arg) => {
console.log('Dropped File(s):', arg);
event.returnValue = `Received ${arg.length} paths.`; // Synchronous reply
})
ondragstart
in your renderer process by sending the filepath usingipcRenderer
? That event won't appear out of thin air. Take a look at Electron's file d'n'd example, maybe that'll help. – Disencumber