Problem I'm trying to send multiple files from the renderer process to the main process using Electron's IPC, but the file objects arrive empty. Renderer Process:
if (files?.length > 1) {
window.electron.ipcRenderer.send('multipleFiles', files)
}
Main Process:
ipcMain.on('multipleFiles', (event, payload) => {
console.log("Received multipleFiles");
console.log(payload);
Object.keys(payload).map((key) => {
console.log(payload[key].data) // undefined or empty
})
})
What's Happening
The iteration runs the correct number of times (matching files.length) However, each element in the payload is just an empty object {} The file data is not being transferred
What I've Tried I can confirm that files contains valid File objects in the renderer process before sending, but they lose their data when received in the main process. Question Why are the File objects arriving empty in the main process, and how can I properly transfer file data through Electron's IPC? Environment:
Electron version: 38.1.2
Reproduction:
Run npm create @quick-start/electron, and then set renderer.js to
<script lang="ts">
const ipcHandle = (): void => window.electron.ipcRenderer.send('ping')
function handleFileSelect(event: Event): void {
const input = event.target as HTMLInputElement
const files = input.files
if (files?.length === 1) {
console.log(`Processing ${files.length} file(s)`)
for (const file of Array.from(files)) {
const reader = new FileReader()
reader.onload = () => {
if (reader.result instanceof ArrayBuffer) {
console.log('About to send:', file.name)
window.electron.ipcRenderer.send('file-upload', {
name: file.name,
type: file.type,
data: reader.result
})
} else {
console.error('Expected ArrayBuffer but got:', typeof reader.result)
}
}
reader.onerror = () => {
console.error('FileReader error:', reader.error)
}
reader.readAsArrayBuffer(file)
}
} else if (files?.length > 1) {
window.electron.ipcRenderer.send('multipleFiles', files)
}
}
</script>
<div class="creator">Powered by electron-vite</div>
<div class="text">
Build an Electron app with
<span class="svelte">Svelte</span>
and
<span class="ts">TypeScript</span>
</div>
<p class="tip">Please try pressing <code>F12</code> to open the devTool</p>
<div class="actions">
<div class="action">
<a href="https://electron-vite.org/" target="_blank" rel="noreferrer">Documentation</a>
</div>
<div class="action">
<a target="_blank" rel="noreferrer" onclick={ipcHandle}>Send IPC</a>
<input type="file" multiple id="sheetSelection" onchange={handleFileSelect} />
</div>
</div>
and add the following to main.js
ipcMain.on('file-upload', (event, payload) => {
console.log("Received file:", payload.name, payload.type)
console.log("Data size:", payload.data.byteLength, "bytes")
// Now you can work with the data
// ... rest of your XLSX processing
})
ipcMain.on('multipleFiles', (events, payload)=>{
console.log("Received multipleFiles");
console.log(payload)
Object.keys(payload).map((key)=>{
console.log(payload[key].data)
})
})
window.electron? The question looks very weird. The entire idea is wrong. The scenarios of using IPC with files are very different. If you explain your ultimate goal, I will be able to explain what to do. A quick hint: everything works the other way around.