I am just looking through the Cordova source code to try and figure something out, and 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
Note While the W3C FileSystem spec is deprecated for web browsers, the FileSystem APIs are supported in Cordova applications with this plugin for the platforms listed in the Supported Platforms list, with the exception of the Browser platform. To get a few ideas how to use the plugin, check out the sample at the bottom of this page.
Cordova - File System. This plugin is used for manipulating the native file system on the user's device. Step 1 - Installing File Plugin. We need to run the following code in the command prompt to install this plugin. Step 2 - Add Buttons. In this example, we will show you how to create file, write to file, read it and delete it.
The OS may delete these files when the device runs low on storage, nevertheless, apps should not rely on the OS to delete files in here. ( iOS, Android, BlackBerry 10, OSX, windows) cordova.file.externalApplicationStorageDirectory - Application space on external storage. ( Android)
In cordova you can actually read an android file with a given path easily ( file://storage/etc/etc.txt) using the cordova file plugin ,for example:
file.fullPath
is a part of the spec, and should actually look like: /path/relative/to/my/root
. If you're seeing that it has file:///
, then that's a bug.
file.nativeURL
is meant to be an implementation detail. But sadly, it's not obviously marked as such. It's not a part of the spec, and doesn't exist on other platforms.
file.toURL()
is likely what you want. It's a part of the spec, gives you a URL that can be passed to resolveLocalFileSystemURL
, and has fewer gotchas compared to cdvfile:
URLs.
file.toInternalURL()
is not a part of the spec, but it a Cordova-specific extension. I don't think it would ever be a useful thing to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With