Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0fdc1ec212be1e29309820477257a0c3 to your computer and use it in GitHub Desktop.
Save anonymous/0fdc1ec212be1e29309820477257a0c3 to your computer and use it in GitHub Desktop.
var text = '';
// Fill the first chunk, except for the last byte
for (var i = 0; i < 256 * 1024 - 1; i++) {
text += '.';
}
// In this example, the reading will split the character into '...\x01' and '\x53',
// which fails to decode in UTF-8
text += '\u0153';
text += '....'; // More padding, not required
function createFile(dirEntry, fileName) {
dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {
writeFile(fileEntry, text);
}, console.error);
}
function writeFile(fileEntry, text) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function() {
console.log("Successful file write...");
readFile(fileEntry);
};
fileWriter.onerror = function(e) {
console.log("Failed file write: " + e.toString());
};
fileWriter.write(text);
});
}
function readFile(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
var data = this.result;
if (data) {
console.log("Successful file read: " + data.length + " " + data.substring(data.length - 10));
}
};
reader.onerror = function() {
console.log('failed to read', this.error);
};
// If this is uncommented, reading works fine:
// file.start = 1;
reader.readAsText(file);
}, console.error);
}
window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function(fs) {
createFile(fs.root, "newTempFile.txt", false);
}, console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment