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 read, write and navigate through the device native file system hierarchies.
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,
switching between catalogs.
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 );
var errorHandler = function(err) { console.log(err); } requestFileSystem( window.PERSISTENT, 10*1024*1024 /*10MB*/, function(fs) { fs.root.getDirectory('foo', null, function(dirEntry) { dirEntry.getDirectory('bar', null, function(dirEntry) { dirEntry.getFile('baz', null, function(fileEntry) { console.log(fileEntry.name); }, errorHandler); }, errorHandler); }, errorHandler); }, errorHandler );