Skip to content

Instantly share code, notes, and snippets.

@vishalkardode
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save vishalkardode/95872bb12f570ee655a7 to your computer and use it in GitHub Desktop.

Select an option

Save vishalkardode/95872bb12f570ee655a7 to your computer and use it in GitHub Desktop.
Cordova File API Example Usage
// Request Directory
function getDirectory(dirName /*this can be directory path also*/) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
// Success Function Callback
fileSystem.root.getDirectory(dirName, {}, function(directory) {
// Success Function Callback
console.log(directory, 'directory');
}, function(error){
//Error CallBack - getDirectory
console.log(error,"Directory Lookup Error ");
});
}, function(error){
// Error Callback - requestFileSystem
// on Android this will print only Error as cordove / android limitations
console.log("Error : ", error);
});
}
// Request File
function getFile(fileName /*this needs be directory path relative to File System root directory*/) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
// Success Function Callback
fileSystem.root.getFile(fileName, {}, function(file) {
// Success Function Callback
console.log(file, 'file');
}, function(error){
//Error CallBack - getDirectory
console.log(error,"Directory Lookup Error ");
});
}, function(error){
// Error Callback - requestFileSystem
// on Android this will print only Error as cordove / android limitations
console.log("Error : ", error);
});
}
// Get FileEntryObject - Dirty Way
function getFileEntry(fileName, parentDirectory /* e.g. Download/ */){
var entry = new FileEntry(fileName);
entry.fullPath = "//"+ parentDirectory + fileName;
entry.nativeURL = fileSystem.root.toURL() + parentDirectory + fileName;
entry.filesystem = new FileSystem('persistent');
return entry;
}
// Get DirectoryEntryObject - Dirty Way
function getDirectoryEntry(dirName, parentDirectory /* e.g. Download/ */){
var entry = new DirectoryEntry(dirName);
entry.fullPath = "//"+ parentDirectory + dirName;
entry.nativeURL = fileSystem.root.toURL() + parentDirectory + dirName;
entry.filesystem = new FileSystem('persistent');
return entry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment