How to check a file's existence in phone directory with phonegap How to check a file's existence in phone directory with phonegap ajax ajax

How to check a file's existence in phone directory with phonegap


I had the same problem. I could not manage to get Darkaico's answer to work, but with the answer of Kurt I could make it work.

Here's my code:

function checkIfFileExists(path){    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){        fileSystem.root.getFile(path, { create: false }, fileExists, fileDoesNotExist);    }, getFSFail); //of requestFileSystem}function fileExists(fileEntry){    alert("File " + fileEntry.fullPath + " exists!");}function fileDoesNotExist(){    alert("file does not exist");}function getFSFail(evt) {    console.log(evt.target.error.code);}

Then you only need to execute like this:

checkIfFileExists("path/to/my/file.txt");


I get a handle to the file using the .getFile('fileName',{create:false},success,failure) method. If I get the success callback the file is there, otherwise any failure implies that there is a problem with the file.


The above answers did not work for me, but this did :

window.resolveLocalFileSystemURL(fullFilePath, success, fail);

from : http://www.raymondcamden.com/2014/07/01/Cordova-Sample-Check-for-a-file-and-download-if-it-isnt-there