Skip to content

Instantly share code, notes, and snippets.

@mnvr
Last active May 2, 2024 15:15
Show Gist options
  • Save mnvr/91eaca21b0651926565274ee80f10ad5 to your computer and use it in GitHub Desktop.
Save mnvr/91eaca21b0651926565274ee80f10ad5 to your computer and use it in GitHub Desktop.
Electron Fiddle to test webUtils.getPathForFile behaviour on Windows
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Paths</title>
</head>
<body>
<input type="file" />
<script>
const input = document.querySelector("input")
input.addEventListener("change", () => {
const file = input.files[0];
const path = window.electron.pathForFile(file);
const posixPath = window.electron.posixPathForFile(file);
console.log({file, path, posixPath});
})
</script>
</body>
</html>
const { app, BrowserWindow } = require('electron')
const path = require('node:path')
app.whenReady().then(() => {
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
})
app.on('window-all-closed', () => app.quit())
const { contextBridge, webUtils } = require("electron")
contextBridge.exposeInMainWorld("electron", {
pathForFile: (file) => webUtils.getPathForFile(file),
posixPathForFile: (file) => {
const path = webUtils.getPathForFile(file);
// On Windows we get back a path with forward slashes.
// Note that we don't have access to the path module in the preload script.
return navigator.platform.toLowerCase().includes("win") ? path.split("\\").join("/") : path
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment