入门
开发指南
- 模块
- 控件基础知识
- 控件树
- 选择器 API
- 布局
- 手势和触摸事件
- W3C APIs 兼容
- Cordova 插件支持
- EcmaScript 6,TypeScript 和 JSX
- Windows 10 支持说明
- 构建 Tabris.js App
- Tabris.js App 补丁
API 参考文档
- app
- device
- fs
- localStorage
- ui
- ActionSheet(操作列表)
- AlertDialog(对话框)
- CanvasContext
- InactivityTimer(闲置定时器)
- NativeObject
- Popup(弹出窗)
- Timer(计时器)
- WidgetCollection
控件
- Action
- ActivityIndicator
- Button
- Canvas
- CheckBox
- CollectionView
- Composite
- Drawer
- ImageView
- NavigationBar
- NavigationView
- Page
- Picker
- ProgressBar
- RadioButton
- ScrollView
- SearchAction
- Slider
- StatusBar
- Switch
- Tab
- TabFolder
- TextInput
- TextView
- ToggleButton
- Video
- WebView
- Widget
自定义控件
fs
Extends NativeObject
The fs
object provides methods to read and write files. All methods are asynchronous and return a promise.
Import this object with “const {fs} = require('tabris');
”
Methods
readDir(path)
Parameters:
- path: string
- the path of the directory to read.
Returns: Promise<string[]>
Reads the contents of a given directory. Returns a promise that resolves on success to an array of the names of the files in the directory excluding ‘.’ and ‘..’. In case of failure, the Promise rejects with an Error.
readFile(path)
Parameters:
- path: string
- the path of the file to read.
Returns: *Promise*
Reads the given file and returns a promise that resolves to the contents of the file on success and rejects with an Error in case of a failure. The file contents are returned as an ArrayBuffer.
readFile(path, encoding)
Parameters:
- path: string
- the path of the file to read.
- encoding: string
- the encoding to use to read text files.
Returns: *Promise*
Reads the given text file and returns a promise that resolves to the contents of the file on success and rejects with an Error in case of a failure. The file contents are returned as a string.
removeFile(path)
Parameters:
- path: string
- the path of the file to remove.
Returns: *Promise*
Removes the given file. Returns a promise that resolves on success and rejects with an Error in case of a failure.
writeFile(path, data)
Parameters:
- path: string
- the path of the file to write.
- data: ArrayBuffer
- the contents to write to the file.
Returns: *Promise*
Writes the given binary contents to the given file. If the file exists, it is overwritten, otherwise it is created. Returns a promise that resolves on success and rejects with an Error in case of a failure.
writeFile(path, text, encoding)
Parameters:
- path: string
- the path of the file to write.
- text: string
- the text to write to the file.
- encoding: string [Optional]
- the encoding to use to write a text file. When omitted,
utf-8
will be used.
- the encoding to use to write a text file. When omitted,
Returns: *Promise*
Writes the given text to the given file using the given encoding or utf-8
if no encoding is specified. If the file exists, it is overwritten, otherwise it is created. Returns a promise that resolves on success and rejects with an Error in case of a failure.
Properties
cacheDir
read-only
Type: string
The path of a directory that the app may use to store cached files. The OS may delete files in this directory when the device runs low on storage. Only use this location for data that can easily be re-created.
This property can only be set on widget creation. Once set, it cannot be changed anymore.
filesDir
read-only
Type: string
The path of a directory that the app may use to store persistent files.
This property can only be set on widget creation. Once set, it cannot be changed anymore.
Example
const {ImageView, fs, ui} = require('tabris');
let file = fs.cacheDir + '/test.png';
let imageView = new ImageView({
centerX: 0, centerY: 0, width: 400, height: 200,
background: '#aaaaaa'
}).appendTo(ui.contentView);
fetch('http://lorempixel.com/400/200/')
.then(res => res.arrayBuffer())
.then(data => fs.writeFile(file, data))
.then(() => imageView.image = file)
.then(() => console.log('image:', file))
.catch(err => console.error(err));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论