Saving files locally with electron

后端 未结 3 1874
深忆病人
深忆病人 2020-11-30 23:32

I have some template files that contain a few variable strings each, I\'d like to build a very simple input form with Electron (http://electron.atom.io/) and I want to save

3条回答
  •  日久生厌
    2020-11-30 23:55

    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.

提交回复
热议问题