Last active
August 29, 2015 14:15
-
-
Save leyluj/c700f88f7d1c22f4116f to your computer and use it in GitHub Desktop.
Cordova File Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Here is what I am using for my Android and IOS apps | |
Keep attention to a couple of things: | |
- Android and IOS have other directorynames for files | |
- Android devices have different root (myFSRootDirectory1 = Samsung Tab 3, msFSRootDirectory2 = Samsung SII) | |
- $cordovaFile functions prefixes all pathnames with root | |
$cordovaFileTransfer functions needs absolute pathnames | |
Here I create the prefixes for File functions and FileTransfer functions for Android and IOS | |
*/ | |
// The $ionicPlatform and ionic.Platorm are from Ionic framework | |
// | |
$ionicPlatform.ready(function() { | |
if (ionic.Platform.isAndroid()) { | |
// If running on Android | |
console.log('cordova.file.externalDataDirectory: ' + cordova.file.externalDataDirectory); | |
// | |
// I use cordova.file.externalDataDirectory because this url is for Android devices | |
// If you remove the app from the device these url are cleared too on the device. So keep it clean. | |
// Remove the root from cordova.file.externalDataDirectory | |
// | |
myFsRootDirectory1 = 'file:///storage/emulated/0/'; // path for tablet | |
myFsRootDirectory2 = 'file:///storage/sdcard0/'; // path for phone | |
fileTransferDir = cordova.file.externalDataDirectory; | |
if (fileTransferDir.indexOf(myFsRootDirectory1) === 0) { | |
fileDir = fileTransferDir.replace(myFsRootDirectory1, ''); | |
} | |
if (fileTransferDir.indexOf(myFsRootDirectory2) === 0) { | |
fileDir = fileTransferDir.replace(myFsRootDirectory2, ''); | |
} | |
console.log('Android FILETRANSFERDIR: ' + fileTransferDir); | |
console.log('Android FILEDIR: ' + fileDir); | |
} | |
if (ionic.Platform.isIOS()) { | |
// if running on IOS | |
console.log('cordova.file.documentsDirectory: ' + cordova.file.documentsDirectory); | |
// I use cordova.file.documentsDirectory because this url is for IOS (NOT backed on iCloud) devices | |
fileTransferDir = cordova.file.documentsDirectory; | |
fileDir = ''; | |
console.log('IOS FILETRANSFERDIR: ' + fileTransferDir); | |
console.log('IOS FILEDIR: ' + fileDir); | |
} | |
if (ionic.Platform.isAndroid() || ionic.Platform.isIOS()) { | |
// | |
// Just functions from the list below one by one ( or chain them) | |
// | |
} | |
}); | |
// Download file from 'http://www.yourdomain.com/test.jpg' to test/one/test.jpg on device Filesystem | |
var hostPath = 'http://www.yourdomain.com/test.jpg'; | |
var clientPath = fileTransferDir + 'test/one/test.jpg'; | |
var fileTransferOptions = {}; | |
$cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions).then (function() { | |
}); | |
// Create dir test | |
$cordovaFile.createDir(fileDir + 'test/').then( function(dirEntry) { | |
}); | |
// Create dir aganin in dir test | |
$cordovaFile.createDir(fileDir + 'test/one/').then( function(dirEntry) { | |
}); | |
// Create empty file test.txt in test/again/ | |
$cordovaFile.createFile(fileDir + 'test/one/test.txt', true).then( function(fileEntry) { | |
}); | |
// List of files in test/again | |
$cordovaFile.listDir(fileDir + 'test/one/').then( function(entries) { | |
console.log('list dir: ', entries); | |
}); | |
// Write some text into file | |
$cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', '').then( function(result) { | |
}); | |
// Read text written in file | |
$cordovaFile.readAsText(fileDir + 'test/one/test.txt').then( function(result) { | |
console.log('readAsText: ', result); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment