gpt4 book ai didi

electron - Electron 为什么不创建文件?

转载 作者:行者123 更新时间:2023-12-03 12:40:50 29 4
gpt4 key购买 nike

我正在 Electron 上构建一个应用程序,现在我试图创建一个简单的文件。
这是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.")
})

});
};

该窗口将按预期方式打开:
enter image description here

然后,我在输入的名称上写了例如:“hello.txt”,然后单击“保存”。

控制台上没有日志,目录中也没有文件

编辑:
与此js,发生相同的():
  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);

编辑:
这是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();
}

最佳答案

基本上,据我了解,“dialog.showSaveDialog((filename)..”)有点阻塞……我通过使用以下方法解决了:

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

关于electron - Electron 为什么不创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61326483/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com