samedi 12 mars 2016

How to replace device db file in phonegap

Hi I am getting device db file and storing in server to make the backup of database of my app the requirement is that if device lost or broke or theft then in that case the user can download the backup of the database from server and can restore it into their device. I believe below code gives me a device db file with specified path with file name.

function doBackup(){
 var defered = $.Deferred();
var version = parseFloat(window.device.version);
    dbPath = cordova.file.applicationStorageDirectory
      +'/app_webview/databases/file__0/1';

     if(version < 4.4) {  

      dbPath =  cordova.file.applicationStorageDirector+'/app_database
     /file__0/0000000000000001.db';
   }  
var fileName = 'Backup_' + new Date().getTime();   

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
   fs.root.getDirectory(backupFolder, {
    create : true,  
    exclusive : false  
       }, function(dir) {      
   window.resolveLocalFileSystemURL(dbPath, function(db) {
      db.copyTo(dir, fileName, function() {
  if(online) {      
     console.log("Im online - "+dbPath);
     readFile(fileName);
     //checkIfDBFileExists(backupFolder+'/log1.txt');
  } else {      
    var data = new BackupData();
    data.FileName = fileName;
    data.BackupDate = new Date().getTime();
    SaveBackupData(data, GetBackupData); 
  }        
   }, onfail)
   });  
   }, onfail);
    }, onfail);

   function onfail(err){
      return defered.reject(err);
     }
 return defered.promise();
     }

For replacing I am trying below code BUT there is no luck

     function  copyFileToDB(fName){  
  var defered = $.Deferred();

// request for file system
 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs){
    //request for directory Entry
    //backup folder name in dd mm yyyy format
    var today = new Date();
    //var fileName = today.getDate()+" " + (today.getMonth()+1) + " "+today.getFullYear();
    var fileName = fName;

    fs.root.getDirectory("Download/SMU/Backup/", { 
        create: false,
        exclusive: false
    }, function(destDirectoryEntry)  
 {window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory 
   +"app_webview/databases/file__0/1",function(fileEntry){     
   fileEntry.copyTo(destDirectoryEntry, fileEntry.name, function(){
                alert("success - "+JSON.stringify(destDirectoryEntry));
            }, onfail);
        },onfail)
    }, onfail);
}, onfail);

function onfail(err){
    return defered.reject(err); 
}
return defered.promise();   
}

Please anybody help me to resolve this..

Aucun commentaire:

Enregistrer un commentaire