Skip to content

Instantly share code, notes, and snippets.

@Montoya
Forked from rally25rs/fileStorage.coffee
Last active February 2, 2020 20:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Montoya/e26847db9e34762b77c9 to your computer and use it in GitHub Desktop.
Save Montoya/e26847db9e34762b77c9 to your computer and use it in GitHub Desktop.
Cordova File API Wrapper
/* 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;
}
};
@Montoya
Copy link
Author

Montoya commented Nov 24, 2015

Example usage:

1st example - writing to a text file, then reading the text back and displaying it

fileStorage.write('test/test.txt','Test File Text').then(function() { 
  fileStorage.read('test/test.txt').then(function(data) { 
    alert("File contains: "+data); 
  }, function(error) { 
    alert("File read failed, error: "+error);  
  }); 
}, function(error) { 
  alert("File write failed, error: "+error); 
}); 

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)

var img = document.getElementById('my_img'); 
var xhr = new XMLHttpRequest(); 
xhr.open('GET',img.src,false); 
xhr.responseType = 'arraybuffer'; 
xhr.onload = function(e) { 
  if(this.status == 200) { 
    fileStorage.write('saved-images/my_img.jpg', this.response).then(function() { 
      alert('Image saved successfully'); 
    }, function(error) { 
      alert('Image was not saved, error: '+error); 
    }); 
  }
  else { 
    alert('Could not load image, error: '+this.responseText); 
  }
}; 
xhr.send(); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment