Copying a file using Cordova

前端 未结 4 1466
旧时难觅i
旧时难觅i 2021-01-19 03:21

I\'ve been trying to copy file named versions.txt from applicationDirectory to externalApplicationStorageDirectory using cordova but code fails.

here is the code

4条回答
  •  旧巷少年郎
    2021-01-19 03:50

    I finally got a functioning broader no specific/case function to copy a file having a baseFileURI to a temporary (fileSystem == LocalFileSystem.TEMPORARY) or persistent (fileSystem == LocalFileSystem.PERSISTENT) APP directory, the destiny file having the name destPathName.

    It uses the cordova-plugin-file plugin.

    function copyFile(baseFileURI, destPathName, fileSystem){
        window.resolveLocalFileSystemURL(baseFileURI, 
            function(file){        
                window.requestFileSystem(fileSystem, 0, 
                    function (fileSystem) {
                        var documentsPath = fileSystem.root;
                        console.log(documentsPath);
                        file.copyTo(documentsPath, destPathName,
                        function(res){                        
                            console.log('copying was successful to: ' + res.nativeURL)
                        }, 
                        function(){
                            console.log('unsuccessful copying')
                        });
                    });
            }, 
            function(){
                console.log('failure! file was not found')
            });
    }
    

    Use this function, for example, like this

    copyFile("file:///storage/emulated/0/Android/data/com.form.parking.violation/cache/IMG-20180505-WA0004.jpg",         
            "myImg.jpg",
            LocalFileSystem.TEMPORARY); 
    

提交回复
热议问题