Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228603

How to access data saved in app folder

$
0
0

I 'download' and access files from the APK using plugins cordova-plugin-file 3.0.0 "File" and cordova-plugin-file-transfer 1.4.0 "File Transfer".

As far as I can see you should be able to do the same with internet files by just changing the URL. Is the following code to any use to you?

var path = 'extra/abcd.txt';
$ionicPlatform.ready(function() {

if (window.cordova) {
    var url = cordova.file.applicationDirectory + 'www/' + path;
    var targetPath = cordova.file.dataDirectory + path.substring(path.lastIndexOf('/') + 1);
    var options = {
        encodeURI: false
    };
    var trustHosts = true;
    // Get file from APK
    $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(
        function(result) {
            $log.log("Successfully got file from APK: " + JSON.stringify(result));
            // File saved to data directory, get it.
            window.resolveLocalFileSystemURL(result.nativeURL,
                function (fileEntry) {
                    // Create file object
                    fileEntry.file(function(file) {
                        $log.log("Got local file");
                        var reader = new FileReader();
                        // Callback called when reading file is complete
                        reader.onloadend = function () {
                            $log.log("File size: " + reader.result.byteLength);
                            var fileContent = reader.result;
                        };
                        // Read file into an ArrayBuffer
                        reader.readAsArrayBuffer(file);
                    },
                    function() {
                        $log.log("Failed to get file from data directory");
                    });
                }
            );
        },
        function(error) {
            $log.log("Failed to fetch file from APK: " + JSON.stringify(error));
        }
    );
}

});


Viewing all articles
Browse latest Browse all 228603

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>