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?
Archived Comments
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.
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.
general guidelines to live under Apple's rules on storage.
how to mark downloaded files to be backed up (or not) on iOS.
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)
Good ones. I added the first two, but I think the third is already covered.
Another mac app that I use for checking files on the iPad is iExplorer ( http://www.macroplant.com/i... )
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
really? I had to cook it up myself since I couldn't find it anywhere.
can you give me a pointer where to look?
@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.)
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.
@Kevin: I like that, but modified it to be a bit more open (metadata). Added to the list.
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.
Hi Raymond,
I have had a generic File API question that has always bugged me (which can be found on stackoverflow: http://stackoverflow.com/qu.... 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
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
@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...
@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-c...
Awesome! Thanks for your help @Raymond and @Greg! I'll check out both of these...
I've started writing here: (removed - doc is now published)
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
Weird - sorry - not sure what to recommend outside of the DD forums.
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
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.
Hi, do you have any example for creating and writing on a txt file, using cordova.file plugin?
Start here, http://www.raymondcamden.co...
For people looking for the doc, it is found here now: https://github.com/cfjedima...
I'd really like to see a simple snippet of code which is a non-platform-non-version-specific way of writing a local file using transfer.download, and then accessing it using the Media plugin.