-
-
Save Montoya/e26847db9e34762b77c9 to your computer and use it in GitHub Desktop.
Cordova File API Wrapper
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
/* modified from https://codingwithspike.wordpress.com/2014/12/29/using-deferreds-with-the-cordova-file-api/ */ | |
/* requires rsvp.js */ | |
/* tested and working in iOS and Android on latest Cordova (5.2.0) and File plugin (4.0.0) */ | |
/* uses dataDirectory which is not synced to iCloud on iOS. You can replace each reference to syncedDataDirectory, but then you will need to set cordova.file.syncedDataDirectory = cordova.file.dataDirectory on Android to maintain compatibility */ | |
window.fileStorage = { | |
write: function (name, data) { | |
var name_arr = name.split('/'); | |
var name_index = 0; | |
var promise = new RSVP.Promise(function(resolve, reject) { | |
var fail = function(msg, error) { | |
reject("Write failed on "+msg+", code: "+error.code); | |
}; | |
var gotFileSystem = function(fileSystem) { | |
if(name_arr.length > 1) { | |
fileSystem.getDirectory(name_arr[name_index], {create:true}, gotDirectory, fail.bind(null, 'gotFileSystem - getDirectory')); | |
} | |
else { | |
fileSystem.getFile(name, { create: true, exclusive: false }, gotFileEntry, fail.bind(null, 'gotFileSystem - getFile')); | |
} | |
}; | |
var gotDirectory = function(directory) { | |
name_index++; | |
if(name_index == (name_arr.length - 1)) { | |
directory.getFile(name_arr[name_index], {create:true, exclusive:false}, gotFileEntry, fail.bind(null, 'gotDirectory - getDirectory')); | |
} | |
else { | |
directory.getDirectory(name_arr[name_index], {create:true}, gotDirectory, fail.bind(null, 'gotDirectory - getFile')); | |
} | |
}; | |
var gotFileEntry = function(fileEntry) { | |
fileEntry.createWriter(gotFileWriter, fail.bind(null, 'createWriter')); | |
}; | |
var gotFileWriter = function(writer) { | |
writer.onwrite = function() { | |
resolve(); | |
}; | |
writer.onerror = fail.bind(null, 'gotFileWriter'); | |
writer.write(data); | |
}; | |
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, gotFileSystem, fail.bind(null, 'requestFileSystem')); | |
}); | |
return promise; | |
}, | |
read: function (name) { | |
var promise = new RSVP.Promise(function(resolve, reject) { | |
var fail = function (msg, error) { | |
reject("Read failed on "+msg+", code: "+error.code); | |
}; | |
var gotFileEntry = function (fileEntry) { | |
fileEntry.file(gotFile, fail.bind(null, 'gotFileEntry')); | |
}; | |
var gotFile = function(file) { | |
reader = new FileReader(); | |
reader.onloadend = function(evt) { | |
data = evt.target.result; | |
resolve(data); | |
}; | |
reader.onerror = fail.bind(null, 'gotFile'); | |
reader.readAsText(file); | |
}; | |
window.resolveLocalFileSystemURL(cordova.file.dataDirectory + name, gotFileEntry, fail.bind(null, 'resolveLocalFileSystemURL')); | |
}); | |
return promise; | |
}, | |
removeFile: function (name) { | |
var promise = new RSVP.Promise(function(resolve, reject) { | |
var fail = function (msg, error) { | |
reject("Remove file failed on "+msg+", code: "+error.code); | |
}; | |
var gotFileEntry = function (fileEntry) { | |
fileEntry.remove(function() { | |
resolve(); | |
}, fail.bind(null, 'remove')); | |
}; | |
window.resolveLocalFileSystemURL(cordova.file.dataDirectory + name, gotFileEntry, fail.bind(null, 'resolveLocalFileSystemURL')); | |
}); | |
return promise; | |
}, | |
removeDirectory: function(name) { | |
var promise = new RSVP.Promise(function(resolve, reject) { | |
var fail = function(msg, error) { | |
reject("Remove directory failed on "+msg+", code: "+error.code); | |
}; | |
var gotDirectory = function(directory) { | |
directory.removeRecursively(function() { | |
resolve(); | |
}, fail.bind(null, 'removeRecursively')); | |
}; | |
window.resolveLocalFileSystemURL(cordova.file.dataDirectory + name, gotDirectory, fail.bind(null, 'resolveLocalFileSystemURL')); | |
}); | |
return promise; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
1st example - writing to a text file, then reading the text back and displaying it
2nd example - saving an image locally
in this case the image's SRC attribute is a Blob URL
notice the data I'm writing is of type "arraybuffer" (a type of bytearray)