Tabris.js 2.0 – Top 10 Features for Mobile Apps Development: File system access
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:
1 2 3 4 5 |
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:
1 2 3 |
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:
- TypeScript & JSX
- Windows 10 Support
- File system access
- AlertDialog
- Binary fetch()
- Simplified event and properties API
- StatusBar and NavigationBar
- Tabris CLI
- Security
- NavigationView
Feedback is welcome!
Want to join the discussion?Feel free to contribute!