本文主要和大家分享微信小程序文件类api详解,希望能帮助到大家。
一.小知识
1.wx.savefile(object):保存文件到本地。
wx.chooseimage({
success: function(res) {
var tempfilepaths = res.tempfilepaths
wx.savefile({
tempfilepath: tempfilepaths[0],
success: function(res) {
var savedfilepath = res.savedfilepath
}
})
}
})
2.wx.getsavedfilelist(object):获取本地已保存的文件列表
wx.getsavedfilelist({
success: function(res) {
console.log(res.filelist)
}
})
3.wx.getsavedfileinfo(object):获取本地文件的文件信息
wx.getsavedfileinfo({
filepath: 'wxfile://somefile', //仅做示例用,非真正的文件路径
success: function(res) {
console.log(res.size)
console.log(res.createtime)
}
})
4.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)
}
})
}
}
})
5.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('打开文档成功')
}
})
}
})
二.列子
3.wx.getsavedfileinfo(object):获取本地文件的文件信息
<view class="container">
<button type="primary" bindtap="upload">上传文件</button>
<text>文件的路径:{{ path}}px</text>
<text>文件大小:{{filesize}}</text>
</view>
//获取应用实例
var app = getapp()
page({
data:{
path:'',
filesize:0,
},
upload:function(){
var that=this
wx.chooseimage({
count: 1,
sizetype: ['original', 'compressed'],// 可以指定是原图还是压缩图,默认二者都有
sourcetype: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var tempfilepaths = res.tempfilepaths;
console.log(tempfilepaths)
wx.getsavedfileinfo({
filepath:res.tempfilepaths[0], //仅做示例用,非真正的文件路径
success: function(res) {
that.setdata({
filesize:res.size,
})
}
})
that.setdata({
path:tempfilepaths
})
}
})
}
})
5.wx.opendocument(object):打开文档
<view class="container">
<button type="primary" bindtap="upload">打开文件</button>
</view>
//获取应用实例
var app = getapp()
page({
data:{
path:'',
},
upload:function(){
var that=this
wx.downloadfile({
url: 'http://192.168.56.1/sino-ui/www.941in.com.hk/m.v1/o.pptx',//文件的在本地的路径
success: function (res) {
var filepath = res.tempfilepath
wx.opendocument({
filepath: filepath,
success: function (res) {
console.log('打开文档成功')
}
})
}
})
}
})
这个文件的路径,必须是http或是https,不能使url: 'd:/www/sino-ui/www.941in.com.hk/m.v1/o.pptx',
相关推荐:
jquery必须掌握的api
php如何开发api接口安全验证实例
php关于api接口实例分享
以上就是微信小程序文件类api详解的详细内容。