Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created April 30, 2014 02:41
Show Gist options
  • Save mkuklis/f2397953e8d7edce87fd to your computer and use it in GitHub Desktop.
Save mkuklis/f2397953e8d7edce87fd to your computer and use it in GitHub Desktop.
filestorage
var storage = {
init: function (success, error) {
window.requestFileSystem(PERSISTENT, 100 * 1024 * 1024, success, error);
},
write: function (fileName, content, success, error) {
this.init(function (fs) {
fs.root.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = callback;
fileWriter.onerror = error;
fileWriter.write(content);
}, error);
}, error);
}, error);
},
read: function (fileName, callback, error) {
this.init(function (fs) {
fs.root.getFile(fileName, {}, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
callback(this.result);
};
reader.onerror = error;
reader.readAsText(file);
}, error);
}, error);
}, error);
},
remove: function (fileName, success, error) {
this.init(function (fs) {
fs.root.getFile(fileName, { create: false }, function (fileEntry) {
fileEntry.remove(success, error);
}, error);
}, error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment