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

时间:2018-12-18 14:49:55

标签: 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(fileName, text, (err) => {
            if(err){
                alert("An error ocurred creating the file "+ err.message)
            }
            alert("The file has been succesfully saved");
        });
    }); 

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

3 个答案:

答案 0 :(得分:1)

您快到了。您需要通过回调等待对话框的结果。检查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");
         });

        }
    }
}

答案 1 :(得分:1)

您可以使用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");
        });
    });

答案 2 :(得分:1)

这对我有用:

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