wx.openDocument偶尔打不开文件
发布于 6 年前 作者 jiangguiying 12893 次浏览 来自 问答
  • 当前 Bug 的表现(可附上截图)

wx.openDocument,成功调用的情况下,偶尔会打开不了文件,再次点击才能打开,开发者工具预览模式下没发现,真机调试有概率触发

  • 提供一个最简复现 Demo

wxml:


<view class="container">

 

  <view class="uploader" bindtap="downloadPdf" style="height:100rpx; line-height: 100rpx;">

    <text>查看pdf</text>

  </view>

 

</view>


js:

downloadPdf: function (url, id) { // 查看pdf

    wx.downloadFile({

      url: 'https://yjd.yxsjob.com/media/2019/1/16/f902367378ea4bfa97d995f08e1049c1/kgSbzLlWbNIxUPFphBNR.pdf',

      success: function (res) {

        const filePath = res.tempFilePath

        wx.openDocument({

          filePath: filePath,

          success: function (res) {

            // console.log('打开文档成功')

          }

        })

      }

    })

  },

2 回复

fail(res){

console.log(res)

}

看看呢

wx.downloadFile({

url: e.currentTarget.dataset.pdf,

header: {},

success: function(res) {

var filePath = res.tempFilePath;

console.log(res);

if (res.statusCode == 404) {

wx.showToast({

title: ‘获取文件失败’,

icon: ‘success’,

duration: 2000

})

} else {

wx.openDocument({

filePath: filePath,

fileType: ‘pdf’,

success: function(res) {

console.log(res);

wx.showToast({

title: “打开成功”,

icon: ‘success’,

duration: 2000

})

},

fail: function(res) {

wx.showToast({

title: “打开失败”,

icon: ‘success’,

duration: 2000

})

},

complete: function(res) {

console.log(res);

}

})

}

},

fail: function(res) {

console.log(‘文件下载失败’);

},

complete: function(res) {},

})

回到顶部