【问题标题】:Copy and replace database file in electron在电子中复制和替换数据库文件
【发布时间】:2018-09-03 22:11:18
【问题描述】:

我正在尝试在电子应用程序中创建备份系统,用户可以在其中选择要使用对话框导入的数据库文件。它应该复制您选择的文件并将其替换为当前文件(如果存在)。我正在使用 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");
  });
});
});
}

我根据一些教程构建了这段代码,它完美地复制了文件内容,但奇怪的是它本身添加了一些标志或其他东西。这会导致该数据库文件已损坏或无法读取。 从替换文件读取后出现此错误。

【问题讨论】:

    标签: angularjs electron


    【解决方案1】:

    我改用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 2015-11-23
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2012-07-13
      • 2014-09-27
      相关资源
      最近更新 更多