从电子中的 SaveFileDialog 获取创建文件的路径

Get created file's path from SaveFileDialog in electron

我试图在使用 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");
        });
    }); 

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

你快到了。您需要通过回调等待对话的结果。检查 docs

所以像这样:

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.showSaveDialog 的同步版本。使用同步版本不需要你声明路径变量而不初始化它

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