Created
December 25, 2013 20:43
-
-
Save ndhu/8126772 to your computer and use it in GitHub Desktop.
FileSystem API write and read text
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
(function (window, document, undefined) { | |
'use strict'; | |
window.FS = window.FS || (function () { | |
//use fallback if fileSystem is prefixed in specifc UA | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
var saveTextFile = function (fileName, value, saveCompleteCallback) { | |
//request the FileSystem, use callback on success (error cb is optional) | |
//@see https://developer.mozilla.org/en-US/docs/Web/API/LocalFileSystem | |
window.requestFileSystem('PERSISTENT', 1024, function (fileSystem) { | |
//The DirectoryEntry at the root of the fs | |
var root = fileSystem.root; | |
root.getMetadata(function (metaData) { | |
console.log("save: metadata of file", fileName, metaData); | |
}); | |
//use the DirectoryEntry at the root to lookup or create a file, use callback when complete | |
//add optional error-callback | |
root.getFile(fileName, {create: true}, function (f) { | |
//we can use this FileEntry to create a writer | |
//@see https://developer.mozilla.org/en-US/docs/Web/API/FileEntry | |
//add optional error-callback | |
f.createWriter(function (fileWriter) { | |
fileWriter.onwriteend = function(e) { | |
if (saveCompleteCallback) { | |
saveCompleteCallback("success", e); | |
} | |
}; | |
fileWriter.onerror = function(e) { | |
if (saveCompleteCallback) { | |
saveCompleteCallback("failed", e); | |
} | |
}; | |
//Blob @see https://developer.mozilla.org/en-US/docs/Web/API/Blob | |
//modern browser desktop support | |
var s = new Blob([value], {type: 'text/plain'}); | |
fileWriter.write(s); | |
}); | |
}); | |
}); | |
}; | |
var loadTextFile = function(fileName, onFileLoaded) { | |
window.requestFileSystem('PERSISTENT', 1024, function(fs) { | |
//The DirectoryEntry at the root of the fs | |
var root = fs.root; | |
//get fileEntry for the fileName | |
//@see https://developer.mozilla.org/en-US/docs/Web/API/FileEntry | |
root.getFile(fileName, {create: false}, function(fileEntry) { | |
fileEntry.file(function(file) { | |
//@see https://developer.mozilla.org/en-US/docs/Web/API/FileReader | |
var fileReader = new FileReader(); | |
fileReader.onloadend = function(e) { | |
console.log("file content: ", this.result); | |
if (onFileLoaded) { | |
onFileLoaded(this.result); | |
} | |
}; | |
fileReader.onerror = function(error) { | |
console.log("error reading file", fileName, error.message); | |
} | |
fileReader.readAsText(file); | |
}); | |
fileEntry.getMetadata (function (metaData) { | |
console.log("load: metadata of file", fileName, metaData); | |
}); | |
}, function(error) { | |
console.log("error getting the file", fileName, error.message); | |
}); | |
}); | |
}; | |
return { | |
saveTextFile: saveTextFile, | |
loadTextFile: loadTextFile | |
}; | |
}()); | |
}(window, document)); | |
var loadTestFile = function (status, obj) { | |
FS.loadTextFile('testFile'); | |
}; | |
FS.saveTextFile('testFile', 'Hello FileSystem', loadTestFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment