Twitter: raymondcamden


Address: Lafayette, LA, USA

PhoneGap/Cordova File System questions

06-23-2014 7,589 views Mobile 25 Comments

A large majority of the questions I get about PhoneGap and Cordova revolve around the file system API. It has had some pretty significant updates recently but still remains one of the most difficult ones to use. I've been putting off really digging deep into those questions because many times they are incredibly specific to a particular user's application. And - honestly - they typically don't lend themselves to the type of quick questions I can answer during the day.

So with that in mind, I thought it might be fruitful to ask my readers about the API and how I can help make things a bit clearer - for both you and me. What I'm thinking is - can we come up with a list of basic questions, a FAQ perhaps, for just this API. It can't be something too precise to one particular application, but something that can apply to multiple users.

As an example: How do I download an asset to my application? That's a simple question, but it brings up the question of where you would download the asset and how you would actually use it later.

Of course, the big issue is that this particular API is an evolving one. Anything I do today will be outdated next year. But as most of my blog posts on the API are already over a year old, this would at least be a bit fresher. ;)

In order to organize this list, I'm going to start it in the blog entry. Use the comments to make suggestions, and as I see ones that I think make sense, I'll add them to the list. By the way, I know some of these are super trivial, but I figure it doesn't hurt to try to cover as much as possible.

File System FAQ

  • When does it make sense to use the file system (versus LocalStorage or WebSQL)?
  • How do I download an asset to my application?
  • How do I use a file stored in the file system (both binary and text-based files)?
  • How do I check to see if a file exists in a directory?
  • Are there external tools that can check a device's file system?
  • How do I check to see if a directory exists?
  • How do I make a subdirectory where the parent directory may not exist?
  • Are there any app store restrictions/guidelines for using the file system?
  • For iOS, where can I store files so that they will be backed up to iCloud?
  • How can I get metadata (size, updated) about a file?

25 Comments

  • Lasse Lundin #
    Commented on 06-24-2014 at 6:14 AM
    Hi Raymond.

    Regarding some of the questions i found a program for my mac to check directories on an iPad.
    It's called iFunBox and shows the directories associated with your app. Really nifty if you want to check if a picture is stored persistently or check if filename is correct.
    Its really easy to use, so hope it helps. Also as I recall it's free.
  • Commented on 06-24-2014 at 9:07 AM
    Oh, that's an excellent suggestion Lasse. If I'm writing code that just downloads crap and I don't need to read it, or I'm not there yet, and I just want to quickly scan the directory, using an external app like that would be much quicker! I'll add it to the list.
  • Kevin Quinn #
    Commented on 06-24-2014 at 12:49 PM
    general guidelines to live under Apple's rules on storage.
  • Kevin Quinn #
    Commented on 06-24-2014 at 12:51 PM
    how to mark downloaded files to be backed up (or not) on iOS.
  • Kevin Quinn #
    Commented on 06-24-2014 at 12:57 PM
    how to reference app content from html you download into Internal/Library space?

    ('../../../AppName.app/www' on iOS and 'file:///android_asset/www' for Android)
  • Commented on 06-24-2014 at 1:53 PM
    Good ones. I added the first two, but I think the third is already covered.
  • Commented on 06-24-2014 at 3:56 PM
    Another mac app that I use for checking files on the iPad is iExplorer ( http://www.macroplant.com/iexplorer/ )
    I have dealt quite a bit with the File API, but these are some that I am not aware of.
    1. File size limit that can be added to the device?
    2. Show progress of file creation?

    I have done these but if you are creating a list, it might be good to have here
    3. Show file download progress
  • Kevin Quinn #
    Commented on 06-24-2014 at 3:56 PM
    really? I had to cook it up myself since I couldn't find it anywhere.

    can you give me a pointer where to look?
  • Commented on 06-24-2014 at 4:02 PM
    @Kevin: I meant your question was already listed. "How do I use a file stored in the file system (both binary and text-based files)?"

    @Greg: I think 1 can be covered in question 2: "How do I download an asset to my application?" Ditto for your second question. To me it is contained in there. (And if it becomes too complex, we break it out.)
  • Kevin Quinn #
    Commented on 06-24-2014 at 5:08 PM
    gotcha. thanks Raymond.

    another one would be "how to check if a file is X minutes old?"

    right now it's broken on Android so maybe list a workaround.
  • Commented on 06-24-2014 at 9:44 PM
    @Kevin: I like that, but modified it to be a bit more open (metadata). Added to the list.
  • Sunoj Vijayan #
    Commented on 06-25-2014 at 2:49 AM
    Hi,

    Please correct me if I am wrong, but it would be nice if we could download a file to specific folder(say 'appimages") inside my application folder.
  • Kelvin Dart #
    Commented on 06-25-2014 at 2:59 AM
    Hi Raymond,

    I have had a generic File API question that has always bugged me (which can be found on stackoverflow: http://stackoverflow.com/questions/23675927/cordov...). But to save a click, the question is as follows:

    I am trying to figure out why there are currently six alternate methods/properties to access the path of a file.

    Currently (running using iOS), there is:
    // Properties
    file.fullPath; // file:///full/path/syntax/file
    file.nativeURL; // file:///full/path/syntax/file

    // Method(s)
    file.toInternalURL(); // formats the file.fullPath into a cdvfile://localhost/persisten/file.
    file.toURL(); // if file.nativeURL is set, uses file.nativeURL, otherwise use file.toInternalURL() or file.fullPath.

    // Deprecated method(s)
    file.toURI(); // deprecated - calls file.toURL();
    file.toNativeURL() // deprecated - calls file.toURL();

    I understand two are deprecated - which both point to file.toURL() - so I can ignore them and focus on just four methods.

    But what is the difference between file.fullPath and file.nativeURL - they are exactly the same? They are both properties on the file object - both publicly accessible.

    As far as I can tell, file.toURL() uses both of these - first file.nativeURL if not that, then file.toInternalURL() or failing that, then file.fullPath.

    Then finally, file.toNativeURL() returns a cdvfile:// formatted location.

    So, most methods point to the file.nativeURL property. Is file.toURL() the method to use since it handles all instances? If so, then what on earth is cdvfile://?

    Thanks,
    Kelvin
  • Nathan Prather #
    Commented on 06-25-2014 at 3:02 AM
    I'm working on a phonegap app right now that will need to allow users to download a zip file from a vendors website and upload it into my app so this information will be very helpful to me... Thanks!
    Nathan
  • Commented on 06-25-2014 at 9:01 AM
    @Sunoj: I believe that is covered in the list already.
    @Nathan: I've done "download a zip and extract" before, but not in PhoneGap. But the code should be very similar: http://www.adobe.com/devnet/html5/articles/real-wo...
  • Commented on 06-25-2014 at 9:18 AM
    @Nathan Here is a PG plugin that might be of use to you. I have used it before and it worked well for my needs - https://github.com/fiscal-cliff/phonegap-plugin-Ex...
  • Nathan Prather #
    Commented on 06-25-2014 at 12:43 PM
    Awesome! Thanks for your help @Raymond and @Greg! I'll check out both of these...
  • Commented on 06-28-2014 at 2:13 PM
    I've started writing here: https://docs.google.com/document/d/1qKB63z3U2BwCl7...
  • amiel asparen #
    Commented on 07-05-2014 at 3:48 AM
    hey i need help. im making an app on a android driven tablet using dreamweaver cc when i used the PHONE GAP BUILD SERVICE to build my project and the i download it and intall it to my device

    i get this respone

    Hey it phone gap!!

    and that it

    o by that way a newbie in the app developing

    need your immediate help T.T
  • Commented on 07-05-2014 at 9:43 AM
    Weird - sorry - not sure what to recommend outside of the DD forums.
  • Lugo #
    Commented on 08-07-2014 at 5:11 PM
    Hi Raymond!

    actually i try create a list of all element of directory whit cordova on my ipad....but a can't!..

    I really frustred......

    I try whit this code:
    function success(entries) {
    var i;
    for (i=0; i<entries.length; i++) {
    console.log(entries[i].name);
    }
    }

    function fail(error) {
    alert("Failed to list directory contents: " + error.code);
    }

    // Get a directory reader
    var directoryReader = dirEntry.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(success,fail);

    but the code is broke in this line "var directoryReader = dirEntry.createReader();".

    Any suggestions?

    PD: Sorry for my bad english :P
  • Commented on 08-08-2014 at 1:22 PM
    This post is not really for generic tech support. I did ask people to make suggestions about things to cover - and directory reading is already there. Please post your question to the PhoneGap google group or Stack Overflow.
  • Román Suárez #
    Commented on 09-24-2014 at 3:08 PM
    Hi, do you have any example for creating and writing on a txt file, using cordova.file plugin?
  • Commented on 09-24-2014 at 3:10 PM
    Start here, http://www.raymondcamden.com/2014/6/23/PhoneGapCor...
  • Commented on 09-29-2014 at 5:46 AM
    For people looking for the doc, it is found here now: https://github.com/cfjedimaster/Cordova-Examples/w...

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty