Steviewevie
Steviewevie

Reputation: 301

Cordova read directory contents

Is there a way to use Cordova to read the contents of a directory? I'm currently working on a remote file browser that will download files to a specific directory.

I want to be able to read the 'Downloads' folder.

Is it possible?

I've found ways of reading+writing files, and writing a directory - but no way of reading the contents of a directory.

Upvotes: 1

Views: 1538

Answers (1)

Gabriel Matusevich
Gabriel Matusevich

Reputation: 3855

to read a directory you can use the $window service

you may need to wrap this in $ionicPlatform.ready(callback)

$window.resolveLocalFileSystemURL(
    'dir_you_want_to_read',
    function (dirEntry) {
        var dirReader = dirEntry.createReader();

        dirReader.readEntries(
            function (entries) {
                console.log(entries); // directory entries
            },
            function (err) {
                console.log(err);
            }
        );

    },
    function (err) {
        console.log(err);
    }
);

resolveLocalFileSystemURL returns a DirEntry, then you can create a reader from that entry to list all content, entries will be any combination of DirEntries and FileEntries.

I couldn't find any documentation about, I figured this out by doing console.log($window) and looking at examples. Here is one example

Upvotes: 1

Related Questions