收藏
回答

安卓版wx.openDocument 打开文件问题

公司名称

个人

MP帐号(邮箱)

347135219@qq.com

开发者微信号

bill766915976

机型(如iPhone 6s plus)

vivo x9

操作系统(如iOS 9.3)

安卓 7.1

是否必现

出现时间

2017-12-05

操作路径(即如何操作可以复现该问题)

调用wx.downloadFile 下载文件并wx.openDocument打开

问题描述(具体问题介绍)

我在小程序页面定义了一个view用于显示文件下载进度,当进度100%时进度条隐藏。在安卓版微信6.5.22版本中点击下载文件,当进度条走到10%左右(不固定,总之没到100%),文档已被打开。点击系统返回按钮发现进度条还在走。


问题截图(客户端问题界面截图)




代码片段截图

进度条代码

<view class="progress-radial progress-{{percentVal}}" hidden='{{fileHidden}}'><text></text></view>

打开文件代码

<view class='weui-panel qiku-panel' hidden="{{jgSearchDataFocus}}">

        <view class="weui-panel__bd">
          <block wx:for="{{jgSearchData.list}}" wx:key="unique">
            <view class="weui-media-box weui-media-box_text" data-pdfurl="{{item.original_link}}" bindtap="openFile">
              <view class="weui-media-box__title weui-media-box__title_in-text">
                {{item.title}}
                <block wx:if="{{item.pdfFlag == 1}}">
                  <image src='/image/iconExcel.png' style="position:absolute;top:3px;right:0px"></image>
                </block>
                <block wx:elif="{{item.pdfFlag == 2}}">
                  <image src='/image/iconDoc.png' style="position:absolute;top:3px;right:0px"></image>
                </block>
                <block wx:else>
                  <image src='/image/iconPdf.png' style="position:absolute;top:3px;right:0px"></image>
                </block>
              </view>
              <view class="weui-media-box__info">
                <view class="weui-media-box__info__meta">{{item.shortName}}</view>
                <view class="weui-media-box__info__meta time">{{item.published_date}}</view>
              </view>
            </view>
          </block>
        </view>
文件js

function openFile(e, that) {
  that.setData({
    flag: false,
    fileHidden: false,
    percentVal: 0
  });
  const downloadTask = wx.downloadFile({
    url: e.currentTarget.dataset.pdfurl, //仅为示例,并非真实的资源
    success: function (res) {
      if (that.data.percentVal == 100) {
        that.setData({
          flag: true,
          fileHidden: true,
          percentVal: 0
        });
        setTimeout(function () {
          wx.openDocument({
            filePath: res.tempFilePath,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }, 1000)
      }
    }
  })
  downloadTask.onProgressUpdate((res) => {
    that.setData({
      percentVal: res.progress
    });
    console.log('下载进度', that.data.percentVal)
    //console.log('已经下载的数据长度', res.totalBytesWritten)
    //console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
  })
}




回答关注问题邀请回答
收藏

3 个回答

  • 安若锦年丶
    安若锦年丶
    2017-12-07

    这个问题在ios 上面不存在,

    const downloadTask = wx.downloadFile({
        url: e.currentTarget.dataset.pdfurl, //仅为示例,并非真实的资源
        success: function (res) {
          that.setData({
            flag: true,
            fileHidden: true,
            percentVal: 0
          });
          setTimeout(function () {
            wx.openDocument({
              filePath: res.tempFilePath,
              success: function (res) {
                console.log('打开文档成功')
              }
            })
          }, 1000)
        }
      })

    这种方法也不行,为啥wx.openDocument 不提供个在打开文件之前执行函数的属性函数呢

    2017-12-07
    有用
    回复
  • 安若锦年丶
    安若锦年丶
    2017-12-07

    download success 哪怕我把 that.data.percentVal == 100 这个判断去掉直接执行也不行

    2017-12-07
    有用
    回复
  • 2017-12-07

    onProgressUpdate 可能会有延迟,建议在downloadFile success的时候就隐藏进度条

    2017-12-07
    有用
    回复
登录 后发表内容