【问题标题】:Cordova check if file in url existsCordova 检查 url 中的文件是否存在
【发布时间】:2016-01-09 10:01:47
【问题描述】:

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

这是带有路径和文件名的代码:

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

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

【问题讨论】:

  • 这个程序在做什么,@Satch3000?

标签: javascript cordova phonegap-build


【解决方案1】:

尝试此链接中的代码: 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);
}

【讨论】:

  • 试过 checkIfFileExists(theurl + fileName);但这会返回该文件不存在时
  • 使用指令alert(cordova.file.dataDirectory)检查cordova.file.dataDirectory的值;.
  • 谢谢!解决这个问题的一组很棒的功能。 +很多
【解决方案2】:

更新: 也适用于 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");
}

找到解决方案here

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 2020-05-22
    • 2020-06-25
    • 2011-05-09
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多