最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cordova check if file in url exists - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Jan 9, 2016 at 10:01 Satch3000Satch3000 49.4k89 gold badges224 silver badges349 bronze badges 1
  • What this program properly doing, @Satch3000? – hubot Commented May 29, 2016 at 10:05
Add a comment  | 

2 Answers 2

Reset to default 11

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);
}

Update: Works on iOS too.

Try:

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");
}

Found the solution here.

Works on Android. Will test on iOS and update this answer.

发布评论

评论列表(0)

  1. 暂无评论