最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron Dialog not saving the file - Stack Overflow

programmeradmin1浏览0评论

Electron version: 1.3.3 Operating system: Ubuntu 14.04

I want to save a XML object into a .xml file with Electron. I try this:

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

A new windows is opening, I fill the name of the file but nothing has been saving.

Electron version: 1.3.3 Operating system: Ubuntu 14.04

I want to save a XML object into a .xml file with Electron. I try this:

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

A new windows is opening, I fill the name of the file but nothing has been saving.

Share Improve this question edited Jan 19, 2021 at 20:56 Max 4,6361 gold badge26 silver badges30 bronze badges asked Aug 22, 2016 at 11:18 AbelAbel 3559 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

it's recommended to use returned path from dialog.showSaveDialog to get filepath in new versions of electron: (which is result.filePath in the below code)

    filename = dialog.showSaveDialog({}
    ).then(result => {
      filename = result.filePath;
      if (filename === undefined) {
        alert('the user clicked the btn but didn\'t created a file');
        return;
      }
      fs.writeFile(filename, content, (err) => {
        if (err) {
          alert('an error ocurred with file creation ' + err.message);
          return
        }
        alert('WE CREATED YOUR FILE SUCCESFULLY');
      })
      alert('we End');
    }).catch(err => {
      alert(err)
    })

The showSaveDialog() API does not save the file for you. You must use the returned path and use Node to save your file.

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

dialog.showSaveDialog({}).then((result) => {
  fs.writeFile(result.filePath, MyFileData, (err) => {
    // file saved or err
  });
}).catch((err) => {
  // err
});
发布评论

评论列表(0)

  1. 暂无评论