文件
保存文件到本地。
wx.chooseImage({
success: function(res) {
var tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],
success: function(res) {
var savedFilePath = res.savedFilePath
}
})
}
})
本地文件存储的大小限制为 10M
基础库 1.4.0 开始支持,低版本需做兼容处理
获取文件信息
wx.getFileInfo({
success(res) {
console.log(res.size)
console.log(res.digest)
}
})
size | Number | 文件大小,单位:B |
digest | String | 按照传入的 digestAlgorithm 计算得出的的文件摘要 |
获取本地已保存的文件列表
获取本地文件的文件信息。此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 wx.getFileInfo 接口。
wx.getSavedFileInfo({
filePath: 'wxfile://somefile', //仅做示例用,非真正的文件路径
success: function(res) {
console.log(res.size)
console.log(res.createTime)
}
})
删除本地存储的文件
wx.getSavedFileList({
success: function(res) {
if (res.fileList.length > 0){
wx.removeSavedFile({
filePath: res.fileList[0].filePath,
complete: function(res) {
console.log(res)
}
})
}
}
})
新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx
wx.downloadFile({
url: 'http://example.com/somefile.pdf',
success: function (res) {
var filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打开文档成功')
}
})
}
})
数据缓存
wx.setStorage({
key:"key",
data:"value"
})
将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
try {
wx.setStorageSync('key', 'value')
} catch (e) {
}
从本地缓存中异步获取指定 key 对应的内容。
wx.getStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
从本地缓存中同步获取指定 key 对应的内容。
try {
var value = wx.getStorageSync('key')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}
异步获取当前storage的相关信息
wx.getStorageInfo({
success: function(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
keys | String Array | 当前storage中所有的key |
currentSize | Number | 当前占用的空间大小, 单位kb |
limitSize | Number | 限制的空间大小,单位kb |
同步获取当前storage的相关信息
try {
var res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
} catch (e) {
// Do something when catch error
}
从本地缓存中异步移除指定 key 。
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
从本地缓存中同步移除指定 key 。
try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}
清理本地数据缓存。
同步清理本地数据缓存
try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}