0

I'm building an app on electron and now I'm trying to create a simple file. Here's the JS:

const app = require("electron").remote;
var dialog = app.dialog;
var fs = require("fs");

document.getElementById('save_project').onclick=() => {

    dialog.showSaveDialog((filename) => {
      if(filename === undefined){
        console.log("You didnt save the file");
        return;
      };  

      var content = "hello there";

      fs.writeFile(filename, content, (err) => {
        if(err) console.log(err);
        alert("The file has been successfully saved.")
      })

    });
};

This window will open as supposed: enter image description here

Then, I wrote for instance: "hello.txt" on the name input and clicked save.

There's no log written on the console neither a file written in the directory

Edit: with this js, happens the same ():

  const fs = require("fs");
  const {dialog} = require("electron").remote;

document.getElementById("save_project").addEventListener("click", () => {

  dialog.showSaveDialog((filename) => {
    if(filename === undefined){
      console.log("nop");
      return;
    }

    fs.writeFile(filename, content, (err) => {
      if(err){
        console.log(err);
        return;
      }

      alert("file created");
    });
  });
}, false);

Edited: Here's the createWindow()

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1920,
    height: 1080,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: true
    },
  });

  const childWindow = new  BrowserWindow ({ width: 1600, height: 800, parent: mainWindow, modal: true, show : false});



  // and load the index.html of the app.
  mainWindow.loadFile("index.html");
  childWindow.loadFile("welcome.html");

  childWindow.once("ready-to-show", () =>  {
  childWindow.show();
  });

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
}
Rui Melo
  • 47
  • 9

1 Answers1

0

Basically, from what i understood, "dialog.showSaveDialog((filename).." this was kinda blocking... i fixed by using:

const {dialog} = require("electron").remote;
let filename = dialog.showSaveDialogSync()
if(filename === undefined){
     console.log("filename undefined");
     return;
}else{
      console.log(filename)
      saveAsToFile(filename, ...);
}
Rui Melo
  • 47
  • 9