Skip to content

Instantly share code, notes, and snippets.

@glynrob
Created February 23, 2013 20:07
Show Gist options
  • Save glynrob/5021129 to your computer and use it in GitHub Desktop.
Save glynrob/5021129 to your computer and use it in GitHub Desktop.
filesystem api add content to file
// add content to a file
function addContent(existFileName){
fs.root.getFile(existFileName, {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
var sentContent=prompt("Add contents to the file "+existFileName,this.result);
if (sentContent!=null && sentContent!=""){
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(trunc) {
fileWriter.onwriteend = null; // Avoid an infinite loop.
var blob = new Blob([sentContent], {type: 'text/plain'});// Create a new Blob and write it to file.
fileWriter.write(blob);
}
fileWriter.onerror = function(e) {
alert('Sorry, this text could not be saved');
};
fileWriter.seek(fileWriter.length); // Start write position at EOF.
fileWriter.truncate(0); // truncate existing so all data is written over
}, errorHandler);
}
};
reader.readAsText(file);
});
}, function(e) {
if (e.code == FileError.NOT_FOUND_ERR){
alert('Filename does not exists'); // should never happen
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment