Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating filename in Save As window when using Electron Dialog module and .showSaveDialog

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?

like image 426
James Cummings Avatar asked Sep 23 '15 02:09

James Cummings


2 Answers

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
});
like image 174
Domenic Barbuzzi Avatar answered Oct 23 '22 21:10

Domenic Barbuzzi


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`,
});
like image 33
Md Mazedul Islam Khan Avatar answered Oct 23 '22 19:10

Md Mazedul Islam Khan