轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

小西瓜的探索之旅:文件操作测试分享,wx.openDocument探索

发布者: ourcrazy | 发布时间: 2018-1-21 12:53| 查看数: 2998| 评论数: 1|帖子模式

一:文件操作:

小程序官方接口如下图:

针对这几个接口,做了个小demo。

  1. // pages/service/file/index.js
  2. Page({
  3. data:{
  4. //URL是是base64位时可以显示图片,但但download下来就有点问题
  5. imgSrc:',
  6. loading:false,
  7. showList:false,
  8. filesList:[]
  9. },
  10. onLoad:function(options){
  11. // 页面初始化 options为页面跳转所带来的参数
  12. },
  13. onReady:function(){
  14. // 页面渲染完成
  15. },
  16. onShow:function(){
  17. // 页面显示
  18. },
  19. onHide:function(){
  20. // 页面隐藏
  21. },
  22. onUnload:function(){
  23. // 页面关闭
  24. },
  25. downloadFile:function(e){
  26. console.log(e)
  27. // e.currentTarget.dataset.src
  28. var that = this;
  29. that.setData({
  30. loading:true,
  31. })
  32. wx.downloadFile({
  33. url: e.currentTarget.dataset.src, //仅为示例,并非真实的资源
  34. success: function(res) {
  35. console.log('success');
  36. console.log(res)
  37. that.setData({
  38. src:res.tempFilePath
  39. })
  40. show('下载成功');
  41. },
  42. fail:function(){
  43. console.log('fail');
  44. },
  45. complete:function(){
  46. console.log('complete')
  47. that.setData({
  48. loading:false,
  49. })
  50. }
  51. })
  52. },
  53. uploadFile:function(){
  54. var that = this;
  55. console.log(that.data.src);
  56. //upload wx img
  57. wx.uploadFile({
  58. url: 'https://String',//暂无可用URL
  59. filePath:that.data.src,
  60. name:'name',
  61. // header: {}, // 设置请求的 header
  62. // formData: {}, // HTTP 请求中其他额外的 form data
  63. success: function(res){
  64. // success
  65. },
  66. fail: function() {
  67. // fail
  68. },
  69. complete: function() {
  70. // complete
  71. }
  72. })
  73. },
  74. toLocal:function(){
  75. /*保存到本地 */
  76. /*saveFile对临时路径只能保存到本地1次。再次保存该临时路径,显示saveFile:fail file not found */
  77. var that =this;
  78. var tempFilePath = that.data.src||'';//that.data.src;//res.tempFilePath
  79. wx.saveFile({
  80. tempFilePath: tempFilePath,
  81. success: function(res1) {
  82. console.log(res1);
  83. var savedFilePath = res1.savedFilePath
  84. show('保存成功')
  85. },
  86. fail:function(res){
  87. console.log('fail')
  88. console.log(res)
  89. showTip();
  90. },
  91. complete: function(res) {
  92. console.log('complete')
  93. console.log(res)
  94. }
  95. })
  96. },
  97. getList:function(){
  98. wx.navigateTo({
  99. url: './files',
  100. success: function(res){
  101. // success
  102. },
  103. fail: function() {
  104. // fail
  105. },
  106. complete: function() {
  107. // complete
  108. }
  109. })
  110. },
  111. openFile:function(){
  112. var URL =';
  113. var that = this
  114. // wx.openDocument({
  115. // filePath: ',
  116. // success: function (res) {
  117. // console.log('打开文档成功')
  118. // },
  119. // fail:function(res){
  120. // console.log('fail')
  121. // console.log(res)
  122. // },
  123. // complete: function(res) {
  124. // console.log('complete')
  125. // console.log(res)
  126. // }
  127. // })
  128. wx.downloadFile({
  129. url: URL,
  130. success: function (res) {
  131. var filePath = res.tempFilePath
  132. wx.openDocument({
  133. filePath: filePath,
  134. success: function (res) {
  135. console.log('打开文档成功')
  136. console.log(res)
  137. show('打开文档成功')
  138. },
  139. fail:function(res){
  140. console.log('fail')
  141. console.log(res)
  142. showTip();
  143. },
  144. complete: function(res) {
  145. console.log('complete')
  146. console.log(res)
  147. }
  148. })
  149. },
  150. fail:function(res){
  151. console.log('fail')
  152. console.log(res)
  153. },
  154. complete: function(res) {
  155. console.log('complete')
  156. console.log(res)
  157. }
  158. })
  159. },
  160. })
  161. export const show=function(tip){
  162. wx.showToast({
  163. title: tip||'成功',
  164. icon: 'success',
  165. duration: 2000
  166. })
  167. }
  168. export const showTip=function(tip){
  169. wx.showModal({
  170. title:'提示',
  171. content:tip||'操作失败!',
  172. showCancel:false,
  173. })
  174. }
  1. <!--pages/service/file/index.wxml-->
  2. <loading hidden="{{!loading}}"/>
  3. <image mode="top" src="{{imgSrc}}"></image>
  4. <button bindtap="downloadFile" data-src="{{imgSrc}}">下载图片</button>
  5. <button bindtap="toLocal">保存到本地</button>
  6. <!--<button bindtap="uploadFile">uploadFile</button>-->
  7. <button bindtap="getList">查看已保存文件列表</button>
  8. <button bindtap="openFile">打开一份在线的的pdf文件</button>
  9. <view class="tip-box">
  10. <view>tip:</view>
  11. <view>1.操作步骤:下载图片->保存到本地->查看已保存文件列表。</view>
  12. <view>2.下载图片的临时路径仅能保存到本地一次。</view>
  13. <view>3.可以多次下载图片,再每次保存到本地,就可以看到多个文件保存的记录。</view>
  14. <view>4.图片仅用于显示。</view>
  15. </view>

uploadFile和downloadFile都是将文件上传/下载后存放至微信的一个临时路径, 
这个tempFilePath在其他接口上就可以用,比如播放录音、展示头像等。

今天试了一下在安卓机子上的下载与文件管理。

下载图片之后,可以看到微信文件夹中有一个tmp_的文件,这个就是下载之后的一个临时路径。

保存文件至本地,用saveFile,得到的文件是将临时文件的tmp_改为了永久文件store_

在小程序上查看已保存的文件列表,调用 getSavedFileList 
可以清晰看到文件列表上的数据,与文件夹上的一样。

其实我是用与描述相反的方式来确定微信保存在本地的文件夹的。 
不太好找,文件夹名称是wxafiles. 
在我手机上是这样,还没在别的手机上试过。 
小程序的测试只能是开发者预览效果,想要换另一部手机看效果,要么换个有开发权限的号登陆开发者工具,要么另一手机上登录微信。。。

二:探索wx.openDocument之旅

今天尝试了一下 wx.openDocument ,微信的官方接口文档对wx.openDocument接口的介绍是酱紫的...

其中filePath 说明:文件路径,可通过 downFile 获得。 
做了两个demo

  1. wx.openDocument({
  2. filePath: ',
  3. success: function (res) {
  4. console.log('打开文档成功')
  5. },
  6. fail:function(res){
  7. console.log('fail')
  8. console.log(res)
  9. },
  10. complete: function(res) {
  11. console.log('complete')
  12. console.log(res)
  13. }
  14. })

测试结果为:file doesn't exist...

what ?! doesn't exist?

把文件放在目录下,再来段代码:

  1. wx.openDocument({
  2. filePath: './123.pdf',
  3. success: function (res) {
  4. console.log('打开文档成功')
  5. },
  6. fail:function(res){
  7. console.log('fail')
  8. console.log(res)
  9. },
  10. complete: function(res) {
  11. console.log('complete')
  12. console.log(res)
  13. }
  14. })

结果,还是doesn't exist.... 
还是乖点吧,跟着demo这样,先download再open

  1. wx.downloadFile({
  2. url: ',
  3. success: function (res) {
  4. var filePath = res.tempFilePath
  5. wx.openDocument({
  6. filePath: filePath,
  7. success: function (res) {
  8. console.log('打开文档成功')
  9. console.log(res)
  10. },
  11. fail:function(res){
  12. console.log('fail')
  13. console.log(res)
  14. },
  15. complete: function(res) {
  16. console.log('complete')
  17. console.log(res)
  18. }
  19. })
  20. },
  21. fail:function(res){
  22. console.log('fail')
  23. console.log(res)
  24. },
  25. complete: function(res) {
  26. console.log('complete')
  27. console.log(res)
  28. }
  29. })

哇塞,这霸气,还是我来感受一下~~~

再看看console出来的信息

至于为什么会这样,我还没弄明白,只是知道目前是这种现象,必须先download一个在线的支持类型的文档文件,才可用 wx.openDocument 打开浏览。 
就算我download一个同目录下的123.pdf也是不行的~~

猜测是,微信不允许文件目录内的文件直接打开,因为小程序本来就大小就限制,放几份文档,这1M还得了?! 
至于用downloadFile去download线上的文档,会不会是将线上的文档通过一层转换改成了微信app所能解析的文件,然后open就可以打开了呢?!

看来说明要改为“文件路径,必须通过 downFile 获得。”

分享一下这种现象而已, 
有大神知道原因的话请指教~

最新评论

secret787 发表于 2022-5-13 07:13
源代码编程教程

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表