Electron Dialog saving file not working

微笑、不失礼 提交于 2020-05-09 06:46:32

问题


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.


回答1:


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');

var savePath = dialog.showSaveDialog({});

fs.writeFile(savePath, fileData, function(err) {
    // file saved or err
});



回答2:


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)
    })


来源:https://stackoverflow.com/questions/39078170/electron-dialog-saving-file-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!