Created
February 23, 2013 20:07
-
-
Save glynrob/5021129 to your computer and use it in GitHub Desktop.
filesystem api add content to file
This file contains hidden or 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
// 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