我正在尝试在 Electron 应用程序中创建备份系统,用户可以在其中选择要使用对话框导入的数据库文件。它应该复制您选择的文件,并用当前文件替换(如果存在)。我正在使用SQLite和angularjs。我的功能看起来像这样
$scope.load = function(){
dialog.showOpenDialog((fileNames) => {
//select file
if(fileNames === undefined){
console.log("No file selected");
return;
}
//read it
fs.readFile(fileNames[0], 'utf8', (err, data) =>{
if(err){
alert("An error ocurred reading the file :" + err.message);
return;
}
//copy file content into existing database file
fs.writeFile(dbLocation, data, (err) =>{
if (err) {
alert("An error ocurred updating the file" + err.message);
console.log(err);
return;
}
alert("The file has been succesfully saved");
});
});
});
}
我基于一些教程构建了此代码,并且它完美地复制了文件内容,但是奇怪的是,它本身并没有添加任何符号。这导致该数据库文件已损坏或无法读取。
从替换的文件读取后,出现此错误。
最佳答案
我改用了fs.copyFile(),并且有效。它的工作原理是这样的:
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
});
资料来源:https://nodejs.org/api/fs.html#fs_fs_copyfile_src_dest_flags_callback
关于angularjs - 复制并替换 Electron 版中的数据库文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480053/