electron js save file dialog code example

Example 1: electron save as dialog

// Electron Save As Dialog
// Syntax: dialog.showSaveDialog([browserWindow, ]options)

const {remote} = require('electron');
var dialog = remote.dialog;

var browserWindow = remote.getCurrentWindow();
var options = {
    title: "Save new file as...",
    defaultPath : "/path/to/new_file.jsx",
    filters: [
        {name: 'Custom File Type', extensions: ['jsx']}
    ]
}

let saveDialog = dialog.showSaveDialog(browserWindow, options);
saveDialog.then(function(saveTo) {
    console.log(saveTo.filePath);
  //>> /path/to/new_file.jsx
})

Example 2: file picker electron

const {dialog} = require('electron').remote;

document.querySelector('#selectBtn').addEventListener('click', function (event) {
    dialog.showOpenDialog({
        properties: ['openFile', 'multiSelections']
    }, function (files) {
        if (files !== undefined) {
            // handle files
        }
    });
});