sjjk001
sjjk001

Reputation: 325

How to select folder or files using electron dialog?

I want to be able to select either whole directory or multi or single files.

I am able to select only one type so file or directory depending what options i use when creating the dialog box. I know I can create two separate option to either select folder or files but it'd be more handy to have it in one dialog box.

//opens dialog window
var selectStl = () => {
    dialog.showOpenDialog({
        title: "Choose STL Models",
        buttonLabel: "Choose Files",
        defaultPath: app.getPath("desktop"),
        properties: ["multiSelections", "openFile", "openDirectory"],
        filters: [
            {name: 'stl', extensions: ['stl']}
        ]
    }, filepath => {
        filepath.forEach(filePath => {
            if(filePath.includes(".stl") || filePath.includes(".STL")){
                stlList.innerHTML += `<option>${filePath}</option>`;
            }else{
                fs.readdir(filePath, (err, files) =>{
                    files.forEach(file => {
                        if(file.includes(".stl") || file.includes(".STL")){
                            var path = filePath + "\\" + file;
                            stlList.innerHTML += "<option>" + path + "</option>";
                        }
                    })
                })
            }           
        })
    })
}

Expected: Should be able to select folder or if I open the folder the files in it.

Actual: I can select directory but if I go into it the files don't appear. Seems like the "openDirectory" property gets more priority.

Upvotes: 1

Views: 4008

Answers (1)

sjjk001
sjjk001

Reputation: 325

I just read this on the docs

"Note: On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set properties to ['openFile', 'openDirectory'] on these platforms, a directory selector will be shown."

I'm going to leave it up in case someone could use this info

Upvotes: 9

Related Questions