I am trying to create backup system in electron app, where user can select database file which he wants to import using dialog. It was supposed to copy the file you choose and replace it with current, if exists. I am using SQLite and angularjs. My function looks like that
$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");
});
});
});
}
I built this code based on few tutorials and it copies file content perfectly, but strangely it adds few signs itself or something. That causes, that database file is corrupted, or is unable to be read from. I get this error after reading from replaced file.
答案 0 :(得分:0)
我使用了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