Tabris.js 2.0 – Top 10 Features: File system access

July 12, 2017 | 2 min Read

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

The new tabris.fs object provides the methods writeFile(path, data), readFile(path), and 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:

[js] 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)); [/js]

Reading a file is equally simple:

[js] fs.readFile(file) .then(data => console.log(data.byteLength, ‘bytes read from’, file)) .catch(err => console.error(err)); [/js]

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:

  1. TypeScript & JSX
  2. Windows 10 Support
  3. File system access
  4. AlertDialog
  5. Binary fetch()
  6. Simplified event and properties API
  7. StatusBar and NavigationBar
  8. Tabris CLI
  9. Security
  10. NavigationView
Ralf Sternberg

Ralf Sternberg

Ralf is a software engineer with a history as Eclipse committer and project lead.

In recent years, he devoted himself to JavaScript technologies and helped pulling off …