我是靠谱客的博主 洁净麦片,这篇文章主要介绍微信小程序文件类API详解,现在分享给大家,希望可以做个参考。

本文主要和大家分享微信小程序文件类API详解,希望能帮助到大家。

一.小知识

1.wx.saveFile(OBJECT):保存文件到本地。


复制代码
1
2
3
4
5
6
7
8
9
10
11
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):获取本地已保存的文件列表


复制代码
1
2
3
4
5
wx.getSavedFileList({ success: function(res) { console.log(res.fileList) } })
登录后复制

3.wx.getSavedFileInfo(OBJECT):获取本地文件的文件信息


复制代码
1
2
3
4
5
6
7
wx.getSavedFileInfo({ filePath: 'wxfile://somefile', //仅做示例用,非真正的文件路径 success: function(res) { console.log(res.size) console.log(res.createTime) } })
登录后复制

4.wx.removeSavedFile(OBJECT):删除本地存储的文件


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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):获取本地文件的文件信息

复制代码
1
2
3
4
5
<view class="container"> <button type="primary" bindtap="upload">上传文件</button> <text>文件的路径:{{ path}}px</text> <text>文件大小:{{filesize}}</text> </view>
登录后复制
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//获取应用实例 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):打开文档

复制代码
1
2
3
<view class="container"> <button type="primary" bindtap="upload">打开文件</button> </view>
登录后复制

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//获取应用实例 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详解的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是洁净麦片最近收集整理的关于微信小程序文件类API详解的全部内容,更多相关微信小程序文件类API详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(159)

评论列表共有 0 条评论

立即
投稿
返回
顶部