I want to filter when choose a file with showOpenDialog on Electron.
I would like to make user able to choose Markdown (md) only.
But it is not respected.
So now User can select a file of all type.
How I can fix it?
My environment is:
ArchLinux
GNOME 46.2
Electron 31.0.1
const { app, BrowserWindow, dialog, ipcMain } = require("electron");
const path = require("node:path");
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile(path.join(__dirname, "index.html"));
return win;
}
app.whenReady().then(() => {
const win = createWindow()
ipcMain.handle('FileChooser', async () => {
const res = await dialog.showOpenDialog(win, { properties: ["openFile"], title: "Select a Markdown", filters: { name: "Markdown", extensions: ["md"] } })
return res;
})
})
The filters
option takes an array of filters. You are not passing an array, you are passing a single filter object.
Wrap the object in []
so you pass an array with one filter object inside.