Will Beddow
Will Beddow

Reputation: 70

Javascript get local file path via a dialog

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

Upvotes: 2

Views: 4149

Answers (1)

justin.m.chase
justin.m.chase

Reputation: 13655

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' ]}));

Upvotes: 4

Related Questions