Javascript 获取创建的文件';在electron中保存文件对话框的路径

Javascript 获取创建的文件';在electron中保存文件对话框的路径,javascript,node.js,electron,filepath,savefiledialog,Javascript,Node.js,Electron,Filepath,Savefiledialog,在使用SaveFileDialog后,我试图存储我创建的文件的路径,以下是我的代码: let path; dialog.showSaveDialog((fileName) => { if (fileName === undefined){ console.log("You didn't save the file"); return; } fs.writeFile(fileNa

在使用SaveFileDialog后,我试图存储我创建的文件的路径,以下是我的代码:

    let path;
    dialog.showSaveDialog((fileName) => {
        if (fileName === undefined){
            console.log("You didn't save the file");
            return;
        }

        fs.writeFile(fileName, text, (err) => {
            if(err){
                alert("An error ocurred creating the file "+ err.message)
            }
            alert("The file has been succesfully saved");
        });
    }); 

我想做的是,在用户创建文件后,他会在变量路径中输入文件的路径,这可能吗?

您就快到了。您需要通过回调等待对话框的结果。检查一下

比如:

let path; 

function saveProjectAs(text) {
    var options = {
        title: "Save project as ",
        message: "Save project as ",
        nameFieldLabel: "Project Name:",
        // defaultPath:  directory to show (optional)
    }

    dialog.showSaveDialog(mainWindow, options, saveProjectAsCallback);

    function saveProjectAsCallback(filePath) {
        // if user pressed "cancel" then `filePath` will be null
        if (filePath) {
         // check for extension; optional. upath is a node package.
            if (upath.toUnix(upath.extname(filePath)).toLowerCase() != ".json") {
                filePath = filePath + ".json"
            }

        path = filePath;

        fs.writeFile(path, text, (err) => {
           if(err){
                alert("An error ocurred creating the file "+ err.message)
            }
           alert("The file has been succesfully saved");
         });

        }
    }
}

您可以使用同步版本的
dialog.showsavedilog
。对于同步版本,无需在不初始化path变量的情况下声明该变量

   let path = dialog.showSaveDialog( {
        title: "Save file",
        filters: [ { name:"png pictures", ext: [ "png" ] } ], // what kind of files do you want to see when this box is opend
        defaultPath: app.getPath("document") // the default path to save file
    });

    if ( ! path ) {
        // path is undefined
        return;
    }

    fs.writeFile( path , text , ( err , buf ) => {
        if ( err )
            return alert("saved");
        return alert("not saved");
    });
或者异步版本

  dialog.showSaveDialog( {
        title: "Save file",
        filters: [ { name:"png pictures", ext: [ "png" ] } ], // what kind of files do you want to see when this box is opend, ( users will be able to save this kind of files )
        defaultPath: app.getPath("document") // the default path to save file
    }, ( filePath ) => {
        if ( ! filePath ) {
            // nothing was pressed
            return;
        }

        path = filePath;
        fs.writeFile( path , text , ( err , buf ) => {
            if ( err )
                return alert("saved");
            return alert("not saved");
        });
    });
这对我很有用:

const { dialog } = require('electron')

dialog.showSaveDialog({
    title: "Save file"
}).then((filePath_obj)=>{
    if (filePath_obj.canceled)
        console.log("canceled")
    else
        console.log('absolute path: ',filePath_obj.filePath);
});

这是获取所选文件路径的唯一方法吗?难道没有一个不安装其他软件包就可以实现的功能吗?请阅读
upath
的功能。您是说选择文件的扩展名还是绝对路径?