PhoneGap/Cordova Tip: Working with files under www and Android

This is a topic that has come up a few times in comments recently but I wanted to post something a bit more explicit. 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.

You can use XHR to read in files from under www. For text files this is rather trivial. For binary data you want to be careful before reading in large amounts of data. Remember that you can work with binary data via Ajax using XHR2 (spec and support levels).

Finally - one problem you may run into is supporting a dynamic list of files. Since you can’t read the directory, if you want to support a random set of assets under www then you would need to ship a file that contains a list of those resources. You would then do an XHR to that file, get the list, and process as you see it.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments