The Tabris.js 2.0 release is now less than a week away (July 18). During the last preparations, we’re counting down the 10 new features in 2.0 that we’re most exited about. Number 3 is a brand new file system API.
Sometimes you need to store binary files on the device’s file system. There are Cordova plug-ins for file system access, but they’re based on
Blob and the
FileReader API which is dated and rather complicated. We thought there should be an easier way to read and write files in a Tabris app.
The fs object
tabris.fs object provides the methods
removeFile(path). All of these methods are asynchronous and return a promise, so you can directly use them with async/await. The
fs object also provides the paths to the two base directories of your app – one for storing files permanently (
filesDir) and one for files that can be easily re-created and may be cleaned up by the system (
cacheDir). Here’s how an
ArrayBuffer is written to a file:
let file = fs.filesDir + '/test.data'; let data = // an ArrayBuffer or a typed array fs.writeFile(file, data) .then(() => console.log(data.byteLength, 'bytes written to', file)) .catch(err => console.error(err));
Reading a file is equally simple:
fs.readFile(file) .then(data => console.log(data.byteLength, 'bytes read from', file)) .catch(err => console.error(err));
The first version of this new API can only read, write, and delete binary files. We’ll extend it over time. Support for text files will be added soon.
Tabris.js 2.0 – Top 10 Features
The file system API is only one of the new features in Tabris 2.0. Be sure to check out the other highlights as well: