Cordova检查url中的文件是否存在

Sat*_*000 7 javascript cordova phonegap-build

我正在使用cordova/phonegap,需要知道文件是否存在

这是包含路径和文件名的代码:

storeUrl = cordova.file.dataDirectory+'myfolder/myfile.mp3';
Run Code Online (Sandbox Code Playgroud)

如何检查此文件是否存在?

hub*_*bot 10

请尝试以下链接中的代码:https: //cordovablogsblogs.wordpress.com/2015/06/10/how-to-check-a-files-existence-in-phone-directory-with-phonegap/.码:

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);
}
Run Code Online (Sandbox Code Playgroud)


uta*_*nna 6

更新:也适用于iOS。

尝试:

function checkIfFileExists(path){
    // path is the full absolute path to the file.
    window.resolveLocalFileSystemURL(path, fileExists, fileDoesNotExist);
}
function fileExists(fileEntry){
    alert("File " + fileEntry.fullPath + " exists!");
}
function fileDoesNotExist(){
    alert("file does not exist");
}
Run Code Online (Sandbox Code Playgroud)

这里找到解决方案。

适用于Android。将在iOS上进行测试并更新此答案。