Javascript get local file path via a dialog

- 1 answer

Ad

I'm writing a database application in node.js via electron. The user needs to be able to select a json file from anywhere on their system, and I need to get the path or in some other way copy it to the program directory. I've tried a variety of different methods but the only thing that even makes a dialog is <input type="file">, but that returns a path with fakepath in it (I believe this is for browser security, but I don't need that since again, electron app). Any help is appreciated

Ad

Answer

Ad

You can use the dialog.showOpenDialog api.

dialog.showOpenDialog([browserWindow, ]options[, callback])

  • browserWindow BrowserWindow (optional)
  • options Object
    • title String
    • defaultPath String
    • filters Array
    • properties Array - Contains which features the dialog should use, can contain openFile, openDirectory, multiSelections and createDirectory
  • callback Function (optional)

Example

const dialog = require('electron').dialog;
console.log(dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]}));
Ad
source: stackoverflow.com
Ad