I use cordova-plugin-file in my ionic app to download images and save to local.
When I run it in emulator or iphone, there is no error, but when I test it in Chrome, it says, cordova is not defined
when I try to access cordova.file.dataDirectory
How can I run cordova-plugin-file in chrome?
function openBrowser() { var url = 'https://cordova.apache.org'; var target = '_blank'; var options = "location = yes" var ref = cordova. InAppBrowser. open(url, target, options); ref. addEventListener('loadstart', loadstartCallback); ref.
By default, your app has permission to write to cordova. file. applicationStorageDirectory and cordova. file.
A cdv-file path is a specific formatted file path that will be resolved into a native file path automatically. A cdv-file path looks like this: cdvfile://localhost/fs-root/path/to/file. Where fs-root is one of the different file system roots that exists in a mobile device.
By default, your app has permission to write to cordova.file.applicationStorageDirectory and cordova.file.externalApplicationStorageDirectory, and the plugin doesn't request permission for these two directories unless external storage is not mounted.
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)
cordova.file.applicationStorageDirectory is read-only; attempting to store files within the root directory will fail. Use one of the other cordova.file.* properties defined for iOS (only applicationDirectory and applicationStorageDirectory are read-only). The encoding parameter is not supported, and UTF-8 encoding is always in effect.
cordova.file.syncedDataDirectory - Holds app-specific files that should be synced (e.g. to iCloud). ( iOS, windows) cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user's ~/Documents directory. ( iOS, OSX)
Cordova will not be available in the browser; only on your device.
In your index.html you should have this script reference:
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
As the note says, the script won't be available during the development. It will be replaced during the build process.
You can try it yourself:
cordova platform add android
then
cordova build
and you should find under platforms\android\assets\www
2 js files: cordova.js and cordova_plugins.js.
Another option is to add browser as platform:
cordova platform add browser
and the run it:
cordova run browser
but you might face a few troubles.
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