2015-11-17 36 views
3

我有一個可以創建文件的工作應用程序。 我正在尋找一種方法,在工作幾個小時後從Cordova應用程序中刪除文件。我似乎無法使其工作。Cordova刪除文件

這裏是創建和刪除文件的代碼:

function crea(){ 
    alert(cordova.file.externalDataDirectory); 
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) { 
     alert("got main dir",dir); 
     dir.getFile("log1.txt", {create:true}, function(file) { 
      alert("got the file", file); 

     }); 
    }); 
     } 



    function del(){ 
var relativeFilePath = cordova.file.dataDirectory; 
var filename = "log1.txt"; 
alert(relativeFilePath); 
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){ 
    // alert(fileSystem.root.fullPath); 
    fileSystem.root.getFile(relativeFilePath + filename, {create:false}, function(fileEntry){ 
     fileEntry.remove(function(file){ 
      alert("File removed!"); 
     },function(){ 
      alert("error deleting the file " + error.code); 
      }); 
     },function(){ 
      alert("file does not exist"); 
     }); 
    },function(evt){ 
     alert(evt.target.error.code); 
}); 

} 

問候。

回答

6

我得到了它的工作原理:

function del() { 

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) { 

     dir.getFile("log1.txt", {create: false}, function (fileEntry) { 
      fileEntry.remove(function (file) { 
       alert("fichier supprimer"); 
      }, function() { 
       alert("erreur suppression " + error.code); 
      }, function() { 
       alert("fichier n'existe pas"); 
      }); 
     }); 


    }); 

} 

感謝

+3

你的錯誤處理程序,需要有傳遞給它的 「錯誤」爲它工作。 –

0

它還適用於只是用requestFileSystem代替resolveLocalFileSystemURL

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { 
    fs.root.getFile('test.txt', { 
     create: false, 
     exclusive: false 
    }, function(fileEntry) { 
     fileEntry.remove(function (file) { 
      // File deleted successfully 
     }, function (err) { 
      console.log(err); // Error while removing File 
     }); 

    }, function(err) { 
     console.log(err) // Error while requesting File. 
    }); 
}, function(err) { 
    console.log(err) // Error while requesting FS 
});