微信小程序之文件类API
2017-05-26 15:13
169 查看
一.小知识
1.wx.saveFile(OBJECT):保存文件到本地。
2.wx.getSavedFileList(OBJECT):获取本地已保存的文件列表
3.wx.getSavedFileInfo(OBJECT):获取本地文件的文件信息
4.wx.removeSavedFile(OBJECT):删除本地存储的文件
5.wx.openDocument(OBJECT):新开页面打开文档,支持格式:doc,
xls, ppt, pdf, docx, xlsx, pptx
二.列子
3.wx.getSavedFileInfo(OBJECT):获取本地文件的文件信息
5.wx.openDocument(OBJECT):打开文档
这个文件的路径,必须是http或是Https,不能使url: 'D:/WWW/sino-ui/www.941in.com.hk/m.v1/o.pptx',
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',
相关文章推荐
- 微信小程序把玩(三十一)wx.uploadFile(object), wx.downloadFile(object) API
- 微信小程序 Record API详解及实例代码
- 微信小程序组件及API的思维导图
- 微信小程序 获取设备信息 API
- 微信小程序把玩 Record API
- 基于微信api Android程序签名+代码混淆
- 从微信小程序重力感应API到requestAnimationFrame探索实现
- 微信小程序 不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
- 微信小程序访问豆瓣电影api400错误解决方法
- 微信小程序学习用demo:仿猫眼电影;使用猫眼api,下拉加载
- 微信小程序把玩(四十)animation API
- 第三篇、微信小程序-网络请求API
- sublime福音:微信小程序组件及API补全插件
- 微信小程序学习笔记(9)--------API
- 微信小程序 网络API Websocket详解
- 微信小程序把玩(三十八)获取设备信息 API
- 微信小程序 canvas API详解及实例代码
- 拿到微信小程序APPID了如何使用https版API中心来开发
- 微信小程序组件:audio及api
- 微信小程序访问豆瓣电影api400错误解决方法