Cordova check if file in url exists

Satch3000 picture Satch3000 · Jan 9, 2016 · Viewed 8.4k times · Source

I am using cordova / phonegap and need to know if a file exists

Here's the code with the path and filename:

storeUrl = cordova.file.dataDirectory+'myfolder/myfile.mp3';

How can I check if this file exists?

Answer

hubot picture hubot · Jan 9, 2016

Try code from this link: https://cordovablogsblogs.wordpress.com/2015/06/10/how-to-check-a-files-existence-in-phone-directory-with-phonegap/. 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);
}