Home » Android » javascript – Cordova File Plugin – Read from www folder?

javascript – Cordova File Plugin – Read from www folder?

Posted by: admin June 16, 2020 Leave a comment


I’m having an issue on Android (not tested on other platforms) where I would like to use the cordova file API to read a file from the /www folder. There are conflicting pieces of information on the internet about whether this is possible or not. I’ve followed an example like:

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/index.html", gotFile, fail);

However I’m getting error 1 (file not found). I can use AJAX to retrieve the file I need but it doesn’t suit my use case. I want to query a directory in the /www folder and read a dynamically named file. Any ideas?

I’d like to have an updated answer, as the one I’ve seen is over 1 year old:
Can you read files in the www folder using PhoneGap.'s JS methods?

Thanks in advance.

How to&Answers:

You can use this function to get all available files in www/ folder

function listDir(path){
    function (fileSystem) {
      var reader = fileSystem.createReader();
        function (entries) {
        function (err) {
    }, function (err) {
//example: list of www/ folder in cordova/ionic app.
listDir(cordova.file.applicationDirectory + "www/");

(The www folder is in readonly mode. You can’t write anything in it. Cordova plugin file documentation explain this. But you can use the copyTo() method of html5 API File to copy your data in another location)


Per Raymond Camden from earlier this year, this isn’t possible with the cordova=file-plugin: http://www.raymondcamden.com/2015/01/21/phonegapcordova-tip-working-with-files-under-www-and-android

First and foremost, you cannot use the File system APIs to work with files under the www folder. The docs for the File plugin incorrectly states that you have Read access to the application directory (which would contain www) but that is incorrect.

But there are several directories that you do have access to using the file plugin. Here’s some sample code to retrieve the contents of a few of them: https://stackoverflow.com/a/29905718/346550