Last active
August 29, 2015 14:13
-
-
Save phase/4c1bc632360944b47fb3 to your computer and use it in GitHub Desktop.
The following code retrieves the file called "log.txt", its contents are read using the FileReader API and appended to a new <textarea> on the page. If log.txt doesn't exist, an error is thrown.
This file contains 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
// Note: The file system has been prefixed as of Google Chrome 12: | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
function errorHandler(e) { | |
var msg = ''; | |
switch (e.code) { | |
case FileError.QUOTA_EXCEEDED_ERR: | |
msg = 'QUOTA_EXCEEDED_ERR'; | |
break; | |
case FileError.NOT_FOUND_ERR: | |
msg = 'NOT_FOUND_ERR'; | |
break; | |
case FileError.SECURITY_ERR: | |
msg = 'SECURITY_ERR'; | |
break; | |
case FileError.INVALID_MODIFICATION_ERR: | |
msg = 'INVALID_MODIFICATION_ERR'; | |
break; | |
case FileError.INVALID_STATE_ERR: | |
msg = 'INVALID_STATE_ERR'; | |
break; | |
default: | |
msg = 'Unknown Error'; | |
break; | |
}; | |
console.log('Error: ' + msg); | |
} | |
function onInitFs(fs) { | |
fs.root.getFile('log.txt', {}, function(fileEntry) { | |
// Get a File object representing the file, | |
// then use FileReader to read its contents. | |
fileEntry.file(function(file) { | |
var reader = new FileReader(); | |
reader.onloadend = function(e) { | |
var txtArea = document.createElement('textarea'); | |
txtArea.value = this.result; | |
document.body.appendChild(txtArea); | |
}; | |
reader.readAsText(file); | |
}, errorHandler); | |
}, errorHandler); | |
} | |
window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment