Write a file to a specific directory with neutralinojs

Viewed 522

I use a web page to create an xml file with javascript. I would like to write xml string into a file in a specific directory on my computer with neutralinojs. Is it possible to open a file saver browser or to specify directly in the code the path of the file which will be saved (or current directory).

Thank you.

2 Answers

You need to specify the directory with the file name as shown in the official example:

Neutralino.filesystem.writeFile('D:\\files\\librairies\\Desktop\\file1.txt','hello world',
    function (data) {
        console.log(data);
    },
    function () {
        console.error('error');
    }
);

You can also open a file saver dialog before writing the file. The following code will open a file saver dialog:

let response = await Neutralino.os.showDialogSave({
  title: 'Save to file'
});

You can use the response to write the file:

//response.selectedEntry holds the chosen file path and name (e.g. "C:/TEST/myFile.txt")
await Neutralino.filesystem.writeFile({
  fileName: response.selectedEntry,
  data: 'my file content'
});

You have to put that code within a async function.

Complete code:

async function saveMyFile()
  let response = await Neutralino.os.showDialogSave({
    title: 'Save to file'
  });
  await Neutralino.filesystem.writeFile({
    fileName: response.selectedEntry,
    data: 'my file content'
  });
}

saveMyFile();
Related