简体   繁体   English

Cordova App:如何获取文件的实际路径

[英]Cordova App : How to get the actual path of file

I'm using Cordova to make android and iOS app, now I would like to check if file already exist in the dirctory. 我正在使用Cordova制作Android和iOS应用程序,现在我想检查文件是否已经存在于dirctory中。

First I download file from server and save it locally using the code below 首先,我从服务器下载文件,并使用下面的代码在本地保存

$scope.downloadFile = function(){

        alert(cordova.file.dataDirectory)
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("http://example.com/files/th/001.mp3");
        var downloadPath = cordova.file.dataDirectory+'001.mp3'; // ANDROID

        fileTransfer.download(
            uri,
            downloadPath,
            function(entry) {
                $scope.savepath = entry.toInternalURL();
                alert("download complete: " + entry.toURL());
                alert('saved at : '+entry.toInternalURL());
            },
            function(error) {
                alert("download error source " + error.source);
                alert("download error target " + error.target);
                alert("upload error code" + error.code);
            },
            false,
            {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
        );
    }//End DownloadFile

and I would like to check if the file already exist using checkIfFileExists(path) method 我想使用checkIfFileExists(path)方法检查文件是否已存在

function checkIfFileExists(path){
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
            //alert('result: '+JSON.stringify(fileSystem.root))
            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);
    }

I checked on my phone, the file is already saved to Android/data/com.myname.myappname/file/001.mp3 我查看了我的手机,该文件已保存到Android/data/com.myname.myappname/file/001.mp3

but the problem is the code always show file does not exist whenever I use the path like 但问题是每当我使用路径时,代码总是显示文件不存在

cordova.file.dataDirectory+'001.mp3';

or cdvfile://localhost/persistent/files/001.mp3 cdvfile://localhost/persistent/files/001.mp3

or 'cdvfile://localhost/files/001.mp3' 'cdvfile://localhost/files/001.mp3'

so I would like to ask that the real path that I need to use to check if the file exist or not. 所以我想问一下我需要用来检查文件是否存在的真实路径。

Please provide me any suggestion. 请给我任何建议。

Regards. 问候。

Do you need to use or CheckFileExists? 你需要使用或CheckFileExists吗? You could try using Phonegap's FileReader method? 您可以尝试使用Phonegap的FileReader方法吗?

var reader = new FileReader();
var fileSource = cordova.file.dataDirectory+'001.mp3'

reader.onloadend = function(evt) {

if(evt.target.result == null) {
   // Null? You still have a problem: file doesn't exist.
} else {
    // Otherwise the file exists. 
}         
};

//Check if the file exists
reader.readAsDataURL(fileSource); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM