#文件操作API#
>wx.saveFile
>将文件保存到本地
>下次启动微信小程序时仍然可以获取到该文件
>本地文件存储大小限制为10MB
属性 | 类型 | 必填 | 说明 |
tempFilePath | string | 是 | 需要保存的文件的临时路径 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
wx.chooseImage({
success: function(res) {
const tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],
success (res) {
const savedFilePath = res.savedFilePath
}
})
}
})
>wx.getSavedFileList
>获取本地文件列表
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
wx.getSavedFileList({
success (res) {
console.log(res.fileList)
}
})
>wx.getSavedFileInfo
>获取本地文件信息
>包含文件创建时间、文件大小、接口调用结果
>此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 wx.getFileInfo() 接口
属性 | 类型 | 必填 | 说明 |
filePath | string | 是 | 文件路径 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
Page({
onload:function(){
wx.getSavedFileListInfo({
success (res) {
console.log(res.fileList)
}
})
}
})
>wx.removeSavedFile
>用来删除本地文件
属性 | 类型 | 必填 | 说明 |
filePath | string | 是 | 需要删除的文件路径 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
Page({
onload:function(){
wx.getSavedFileList({
success(res){
var fileList=res.fileList;
console.log(fileList)
var file=fileList[0]
wx.getSavedFileListInfo({
filePath:file.filePath
success (res) {
console.log(res.fileList)
}
})
}
})
}
})
>wx.openDocument打开文档
>可以打开doc、xls、ppt、pdf、docx、xlsx、pptx等多种 格式的文档
>新开页面打开文档
属性 | 类型 | 必填 | 说明 |
filePath | string | 是 | 文件路径,可通过 downloadFile 获得 |
fileType | string | 否 | 文件类型,指定文件类型打开文件 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
page({
onload:function(){
wx.downloadFile({
url: 'http://example.com/somefile.pdf',
success: function (res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打开文档成功')
}
})
}
})
}
})
>wx.getFileInfo获取文件信息
>用来获取文件的大小和摘要信息
属性 | 类型 | 必填 | 说明 |
filePath | string | 是 | 本地文件路径 |
digestAlgorith m | string | 否 | 计算文件摘要的算法 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
page({
onload:function(){
wx.getFileInfo({
filePath: filePath,
complete: function (res) {
console.log('打开文档成功')
}
})
}
})
#数据缓存API#
>数据缓存到本地
>微信小程序为数据缓存到本地提供了两种方式:
>wx.setStorage(OBJECT)异步方式
>异步方式将数据存储在本地缓存中指定的key中
>会覆盖原来该key对应的内容
>单个 key 允许存储的最大数据长度为1MB
属性 | 类型 | 必填 | 说明 |
key | string | 是 | 本地缓存中指定的 key |
data | any | 否 | 需要存储的内容。只支持原生类型、Date、及能够通过 JSON.stringify序列化的对象。 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
Page({
onload:function(){
var user=this.getUserInfo();
console.log(user);
wx.setStorage({
key:'user',
data: user,
success: function(res){
console.log(res);
}
})
},
getUserInfo:function(){
var user=new Object();
user.name='xiaogang';
user.sex= '男';
user.age=30;
user.address='北京市';
return user;
}
})
>wx.setStorageSync(KEY, DATA)同步方式
>同步方式将数据存储在本地指定的key中
>会覆盖原来该key对应的内容
>相比于异步缓存数据,它更简单
属性 | 类型 | 必填 | 说明 |
key | string | 是 | 本地缓存中指定的 key |
data | Object/ String | 是 | 需要存储的内容。只支持原生类型、Date、及能够通过 JSON.stringify序列化的对象。 |
示例代码:
Page({
onload: function () {
var userSync = this.getUserInfo();
wx.setStorageSync('userSync', userSync)
},
getUserInfo: function () {
var user = new Object();
user.name ='xiaogang';
user.sex = '男';
user.age = 30;
user.address ='北京市';
return user;
}
})
>数据存储上限为10MB
>获取本地缓存数据
>获取本地缓存数据提供4个API:
>wx.getStorage(OBJECT)
属性 | 类型 | 必填 | 说明 |
key | string | 是 | 本地缓存中指定的 key |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
Page({
onLoad: function() {
wx.getStorage({
key: 'user',
success:function(res){
console.log(res);
}
})
}
})
>wx.getStorageSync(OBJECT)
>同步接口,用来从本地缓存中同步获取指定 key对应的内容
属性 | 类型 | 必填 | 说明 |
key | string | 是 | 本地缓存中指定的 key |
示例代码:
Page({
onLoad: function() {
var userSync=wx.getStorageSync('userSync');
console.log(userSync);
}
})
>wx.getStorageInfo(OBJECT)
>用异步方式获取当前storage的相关信息,来 获取所有key的值
属性 | 类型 | 必填 | 说明 |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用 成功、失败都会执行) |
示例代码:
Page({
onLoad: function() {
wx.getStorageInfo({
success:function(res){
console.log(res);
}
})
}
})
>wx.getStorageInfoSync(OBJECT)
示例代码:
Page({
onLoad: function() {
var storage=wx.getStorageInfoSync();
console.log(storage);
}
})
>移除和清理本地缓存数据
>该部分主要包含四个接口:
>wx.removeStorage(OBJECT) :从本地缓存中移除指定key
>wx.removeStorageSync(KEY):从本地缓存中移除指定key
>wx.clearStorage():清理本地数据缓存
>wx.clearStorageSync() :清理本地数据缓存
...持续学习完善中。
#学无止境#