微信小程序之文件类API
来源:互联网 发布:神马seo搜索排名优化 编辑:程序博客网 时间:2024/06/06 19:37
一.小知识
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',
阅读全文
0 0
- 微信小程序之文件类API
- 小程序文档整理之 -- API(文件)
- 微信小程序开发手记之六:API
- 微信小程序7 文件和数据缓存api
- Moodle 中文 API 之 文件管理API
- 微信小程序之音乐控制播放(audio的API)
- 微信小程序API之网络(一)发起请求
- 微信小程序API之网络(二)上传数据
- 微信小程序API之网络(三)下载数据
- 微信小程序API之网络(四)WebSocket
- 微信小程序 Image API
- 微信小程序 Audio API
- 微信小程序 Video API
- 微信小程序 Storage API
- 微信小程序 location API
- 微信小程序 navigation API
- 微信小程序 animation API
- 微信小程序 canvas API
- HAWQ取代传统数仓实践(九)——维度表技术之退化维度
- 算法竞赛入门经典(第2版)习题5-6 对称轴(Symmetry) Uva1595
- 修改注册表快速新建txt文本文档
- MySQL数据库MVCC多版本并发控制简介
- 工作两年和研究生两年(专业硕士)有什么差异?
- 微信小程序之文件类API
- ubuntu 16.04安装WPS出现系统缺失字体
- pythonpath
- 【AngularJS】panel基本用法——最基本的panel
- 移动前端之flexbox
- keil MDK C++编程编译方法简介
- 2017年4月问题记录与总结
- Hive表操作及管理
- Hbase 集群搭建 干货