When using the Dialog module in Electron and .showSaveDialog() to save a file, is there a way to make the filename in the Save As window populate with the actual filename?
This can be accomplished with the defaultPath
property in dialog.showSaveDialog()
.
It should be noted that, since it's the defaultPath
, you must specify the full file path, not just the name+extension:
dialog.showSaveDialog(
{ defaultPath: '/Users/username/Documents/my-file.txt' },
function (fileName) {
// do your stuff here
});
According to Electron Docs, defaultPath
String (optional) - Absolute directory path, absolute file path, or file name to use by default.
This means if you just pass the file name in the defaultPath
like the following without using the absolute path, it will still work.
dialog.showSaveDialog({
defaultPath: `HelloWorld.txt`,
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With