使用FileSystem API

时间:2016-04-25 12:59:52

标签: android sqlite cordova mime-types html5-filesystem

您好我有一个任务是读取现有设备sqlite .db文件并以二进制格式写入并将数据存储到我的服务器中。每当我需要它从服务器恢复时我都可以用设备替换数据.db文件。 对于POC(概念证明)我已在本地完成(在sdcard中存储.db文件并将其替换为设备现有的.db文件,其工作正常但是我的问题是将.db文件作为base64 mime类型读取并存储它可以存储到服务器。我可以使用特定的mime类型存储PDF和图像,如image / jpeg,application / pdf。 相信我这几天我一直在谷歌搜索但是没有任何运气

直到现在我已尝试使用以下代码

var dbPath;
 function doBackupFile() {
 var version = parseFloat(window.device.version);
 dbPath = cordova.file.applicationStorageDirectory
  +'/app_webview/databases/file__0/1';
  if(version < 4.4) {  
    dbPath = cordova.file.applicationStorageDirectory
   +'/app_database/file__0/0000000000000001.db';
  }        
  //alert("dbPath "+dbPath);
 window.resolveLocalFileSystemURL(dbPath,
        function(fs) {
            var parent = cordova.file.externalRootDirectory
                    + "Download/SMU/Backup";
            //var newName = "myDb.db";
             var newName = 'Backup_' + new Date().getTime();   
            window.resolveLocalFileSystemURL(parent, function(
                    directoryEntry) {
                fs.copyTo(directoryEntry, newName, function() {
                    alert("Backup Success");

                      readFile(newName);


                }, failFiles);
            });
        }, failFiles);

function failFiles(error) {
    if (error.code == FileError.NOT_FOUND_ERR)
        alert("Message : NOT_FOUND_ERR")
    else if (error.code == FileError.SECURITY_ERR)
        alert("Message : SECURITY_ERR")
    else if (error.code == FileError.ABORT_ERR)
        alert("Message : ABORT_ERR")
    else if (error.code == FileError.NOT_READABLE_ERR)
        alert("Message : NOT_READABLE_ERR")
    else if (error.code == FileError.ENCODING_ERR)
        alert("Message : ENCODING_ERR")
    else if (error.code == FileError.NO_MODIFICATION_ALLOWED_ERR)
        alert("Message : NO_MODIFICATION_ALLOWED_ERR")
    else if (error.code == FileError.INVALID_STATE_ERR)
        alert("Message : INVALID_STATE_ERR")
    else if (error.code == FileError.SYNTAX_ERR)
        alert("Message : SYNTAX_ERR")
    else if (error.code == FileError.INVALID_MODIFICATION_ERR)
        alert("Message :  INVALID_MODIFICATION_ERR")
    else if (error.code == FileError.QUOTA_EXCEEDED_ERR)
        alert("Message : QUOTA_EXCEEDED_ERR")
    else if (error.code == FileError.PATH_EXISTS_ERR)
        alert("Message : PATH_EXISTS_ERR")
   }
   }



function readFile(fileName, firstTime) {
console.log("readFile "+fileName);
 var defered = $.Deferred();
console.log('Start reading the file ::: ' + fileName); 
 window.resolveLocalFileSystemURL
(cordova.file.externalRootDirectory + '/' + 
backupFolder + '/' + fileName, function(fileEntry){
console.log('Got File Entry...');
fileEntry.file(function(file){
  console.log('Reading the file...');
  var reader = new FileReader();
  reader.onloadend = function(e) {

  var data_db = e.target.result;
  console.log('File read completed - '+data_db);

    };     
   reader.readAsDataURL(file);
   }, onfail);
  });  
 function onfail(err){
  console.log('Error while reading the file ::: ' + JSON.stringify(err));
    return defered.reject(err);
}
return defered.promise();

}

我正在读取.db文件作为readAsDataURL()方法

请检查我得到的控制台 文件阅读完成 -

File read completed - data:null;base64,U1FMaXRlIGZvcm1hdCAzAAQAAQEAQCAgAAAADgAAAFUAAAAAAAAAAAAAABoAAAAEAAAAAAAAAB8AAAABAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAC3mBwUAAAAIA9gAAAAAKwP7A/YD8QPsA+cD4gPdA9gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBsAAAAnGAAAACUTAAAAIQ8AAAAjDAAAACoJAAAAJAcAAAAiBQEAAAAAAQAAAAABAAAAAAEAAAAAAQAAAAABAAAAAAEAAAAAAQAAAAABAAA

它继续高达120098行,我在这里仅粘贴了2%的控制台,因为SO体仅限于3000个字符..我不知道天气这个二进制格式数据是否有效.db文件。而且我正在获取数据:开头为null,对于image和pdf我们正在获取各自的mime类型.. 请任何人解释我在.db文件中出错的地方

0 个答案:

没有答案