The Filesystem API allows developers to access the device native file system to store and retrieve assets such as images and media files. This JavaScript API provides access to create, read, and write.
The following set of code examples illustrates the most common use case scenarios of using the Filesystem API.
The examples include the following data operations:
creating a file
writing to a file
reading a file
For more code examples and detailed information refer to the W3C website.
var errorHandler = function(err) { console.log(err); } requestFileSystem( window.PERSISTENT, 10*1024*1024 /*10MB*/, function(fs) { fs.root.getFile("foo.txt", { 'create' : true }, function(fileEntry) { console.log(fileEntry.name); }, errorHandler); }, errorHandler );
var errorHandler = function(err) { console.log(err); } var blob = new Blob(["Some text"]); requestFileSystem( window.PERSISTENT, 10*1024*1024 /*10MB*/, function(fs) { fs.root.getFile('foo.txt', { 'create' : true }, function(fileEntry) { var writer = fileEntry.createWriter(); writer.write("My example of text written to file"); writer.onwrite = function(event) { console.log("Done"); }; writer.onerror = function(event) { console.log("Error occured."); writer.write(blob); } }, errorHandler); }, errorHandler );
var errorHandler = function(err) { console.log(err); } requestFileSystem( window.PERSISTENT, 10*1024*1024 /*10MB*/, function(fs) { fs.root.getFile('foo.txt', {}, function(entry) { entry.file(function(fd) { var reader = new FileReader(); reader.onload = function(event) { console.log("Read: " + reader.result); }; reader.onerror = function(event) { console.log("Error occured."); }; reader.readAsText(fd); }, errorHandler); }, errorHandler); }, errorHandler );