wx.savefile(object)
保存文件到本地。
object参数说明:
[tr]参数类型必填说明[/tr]
tempfilepath string 是 需要保存的文件的临时路径
success function 否 返回文件的保存路径,res = {savedfilepath: '文件的保存路径'}
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
示例代码:
wx.startrecord({
success: function(res) {
var tempfilepath = res.tempfilepath wx.savefile({
tempfilepath: tempfilepath,
success: function(res) {
var savedfilepath = res.savedfilepath
}
})
}
}) wx.getsavedfilelist(object)
获取本地已保存的文件列表
object参数说明:
[tr]参数类型必填说明[/tr]
success function 否 接口调用成功的回调函数,返回结果见success返回参数说明
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
success返回参数说明:
[tr]参数类型说明[/tr]
errmsg string 接口调用结果
filelist object array 文件列表
filelist中的项目说明:
[tr]键类型说明[/tr]
filepath string 文件的本地路径
createtime number 文件的保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
size number 文件大小,单位b
示例代码:
wx.getsavedfilelist({
success: function(res) {
console.log(res.filelist)
}
}) wx.getsavedfileinfo(object)
复制代码
获取本地文件的文件信息
object参数说明:
[tr]参数类型必填说明[/tr]
filepath string 是 文件路径
success function 否 接口调用成功的回调函数,返回结果见success返回参数说明
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
success返回参数说明:
[tr]参数类型说明[/tr]
errmsg string 接口调用结果
size number 文件大小,单位b
createtime number 文件的保存是的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
示例代码:
wx.getsavedfileinfo({
filepath: 'wxfile://somefile',
//仅做示例用,非真正的文件路径
success: function(res) {
console.log(res.size) console.log(res.createtime)
}
}) wx.removesavedfile(object)
删除本地存储的文件
object参数说明:
[tr]参数类型必填说明[/tr]
filepath string 是 需要删除的文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
示例代码:
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
object参数说明:
[tr]参数说明必填说明[/tr]
filepath string 是 文件路径,可通过 downfile 获得
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
示例代码
wx.downloadfile({
url: 'http://example.com/somefile.pdf',
success: function (res) {
var filepath = res.tempfilepath
wx.opendocument({
filepath: filepath,
success: function (res) {
console.log('打开文档成功')
}
})
}
})
更多微信小程序:文件管理 api说明。