66

I have some template files that contain a few variable strings each, I'd like to build a very simple input form with Electron (https://www.electronjs.org/) and I want to save the composed output file on the user's computer.

Is there any module I can use to let Electron save files locally?

2
  • 2
    fs?
    – Zen
    Jun 28, 2016 at 5:07
  • @Zen Is there any way to create multiple directories (i.e. path/dir, path/dir2, ...) with one command or is the only way to loop mkdir?
    – oldboy
    Oct 5, 2019 at 6:30

3 Answers 3

68

If you are targeting multiple platforms, I answered a similar question here. Basically app.getPath(name), app.setPath(name, path), and app.getAppPath() are very useful in saving files to the the right place regardless of the OS.

You may also want to check out these Nodejs packages which help simplify saving files directly to the host machine...

If you intend for users to save files you might also have a look at the Dialog api where you can specifically invoke a save dialog for that purpose.

1
  • 1
    I used app.getAppPath to create a file. You are not allowed to create files in the folder that hosts your electron app.
    – Johann
    Feb 12, 2017 at 14:55
34

A sample code is :

const fs = require('fs');
try { fs.writeFileSync('myfile.txt', 'the text to write in the file', 'utf-8'); }
catch(e) { alert('Failed to save the file !'); }

You can of course store the file's name as well as the content's name in variables.

This will save the content in myfile.txt, which is located inside the current working directory (which you can get through process.cwd()). If you want to write, let's say in the user's home directory, you can use the app.getPath function.

7
  • 7
    Where should this save to? Didn't seem to work for me. Jul 18, 2017 at 18:11
  • how to define specific path for saving the file Sep 1, 2017 at 12:43
  • What do you mean? Sep 1, 2017 at 18:26
  • 1
    If you specify a relative path, like I did, it will save on the path you gave under the current directory (let's say for instance C:\MyIncredibleFolder\myfile.txt). If you give an absolute path, it will save the file under the exact path you give. Also, if you want to know in what the relative path you give will result, you can print the result of require('path').join(process.cwd(), "my/relative/path.txt") Dec 27, 2017 at 21:22
  • 2
    This raises more questions then it answers. Dec 21, 2020 at 14:41
11
const {dialog} = require('electron').remote;
var fs = require('fs');

export default {
    methods: {
        save: function () {
            var options = {
                title: "Save file",
                defaultPath : "my_filename",
                buttonLabel : "Save",

                filters :[
                    {name: 'txt', extensions: ['txt']},
                    {name: 'All Files', extensions: ['*']}
                ]
            };

            dialog.showSaveDialog(null, options).then(({ filePath }) => {
                fs.writeFileSync(filePath, "hello world", 'utf-8');
            });
        },
    }
}
1
  • Excellent; thank you for this. I didn't realize Electron had these functions built-in. The new File System Access API was lacking for my use case because I couldn't get the full selected path. Jul 9, 2021 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.