Last active
May 2, 2024 15:15
-
-
Save mnvr/91eaca21b0651926565274ee80f10ad5 to your computer and use it in GitHub Desktop.
Electron Fiddle to test webUtils.getPathForFile behaviour on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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