微信小程序7 文件和数据缓存api

来源:互联网 发布:java 分布式事务 编辑:程序博客网 时间:2024/06/08 15:49

文件

wx.saveFile(OBJECT)

保存文件到本地。

wx.chooseImage({  success: function(res) {    var tempFilePaths = res.tempFilePaths    wx.saveFile({      tempFilePath: tempFilePaths[0],      success: function(res) {        var savedFilePath = res.savedFilePath      }    })  }})
本地文件存储的大小限制为 10M


wx.getFileInfo(OBJECT)

基础库 1.4.0 开始支持,低版本需做兼容处理

获取文件信息

wx.getFileInfo({    success(res) {        console.log(res.size)        console.log(res.digest)    }})
sizeNumber文件大小,单位:BdigestString按照传入的 digestAlgorithm 计算得出的的文件摘要

wx.getSavedFileList(OBJECT)

获取本地已保存的文件列表


wx.getSavedFileInfo(OBJECT)

获取本地文件的文件信息。此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 wx.getFileInfo 接口。

wx.getSavedFileInfo({  filePath: 'wxfile://somefile', //仅做示例用,非真正的文件路径  success: function(res) {    console.log(res.size)    console.log(res.createTime)  }})


wx.removeSavedFile(OBJECT)

删除本地存储的文件

wx.getSavedFileList({  success: function(res) {    if (res.fileList.length > 0){      wx.removeSavedFile({        filePath: res.fileList[0].filePath,        complete: function(res) {          console.log(res)        }      })    }  }})

wx.openDocument(OBJECT)

新开页面打开文档,支持格式: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(OBJECT)

将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口

wx.setStorage({  key:"key",  data:"value"})

wx.setStorageSync(KEY,DATA)

将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。

try {    wx.setStorageSync('key', 'value')} catch (e) {    }

wx.getStorage(OBJECT)

从本地缓存中异步获取指定 key 对应的内容。

wx.getStorage({  key: 'key',  success: function(res) {      console.log(res.data)  } })

wx.getStorageSync(KEY)

从本地缓存中同步获取指定 key 对应的内容。

try {  var value = wx.getStorageSync('key')  if (value) {      // Do something with return value  }} catch (e) {  // Do something when catch error}

wx.getStorageInfo(OBJECT)

异步获取当前storage的相关信息

wx.getStorageInfo({  success: function(res) {    console.log(res.keys)    console.log(res.currentSize)    console.log(res.limitSize)  }})

keysString Array当前storage中所有的keycurrentSizeNumber当前占用的空间大小, 单位kblimitSizeNumber限制的空间大小,单位kb


wx.getStorageInfoSync

同步获取当前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}


wx.removeStorage(OBJECT)

从本地缓存中异步移除指定 key 。

wx.removeStorage({  key: 'key',  success: function(res) {    console.log(res.data)  } })

wx.removeStorageSync(KEY)

从本地缓存中同步移除指定 key 。

try {  wx.removeStorageSync('key')} catch (e) {  // Do something when catch error}


wx.clearStorage()

清理本地数据缓存。

wx.clearStorageSync()

同步清理本地数据缓存

try {    wx.clearStorageSync()} catch(e) {  // Do something when catch error}






原创粉丝点击