openDocument打开错误
发布于 6 年前 作者 guiying04 5347 次浏览 来自 问答

你好,我们有国外的同事在使用小程序的时候,用安卓下载并打开 pdf 文件会提示这个错误,错误如图,代码如下所示,请问是什么原因,国内安卓和海外 ios 没问题

打了 log,下载完显示指定目录文件是有大小的,但是去文件管理器里却找不到,国内安卓可以使用,并且能在文件管理器里找到

var that = this
    var ticketLocalPath = wx.env.USER_DATA_PATH + '/1.pdf'
    console.log(ticketLocalPath)
    wx.showLoading({
      title: '票券下载中',
    })
    wx.downloadFile({
      url: 'https://www.2paynow.cn/zhifu/merchant_material/eTicket2/472200101/tickets/ticket_190404105200_184030085.pdf',
      success: function(res) {
        console.log(res)
        var tempPath = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
        wx.getFileSystemManager().getFileInfo({
          filePath: tempPath,
          success: function(res) {
            console.log(res)
          },
        })
        wx.getFileSystemManager().saveFile({
          tempFilePath: tempPath,
          filePath: ticketLocalPath,
          success: function(res) {
            console.log('下载成功')
            wx.openDocument({
              filePath: ticketLocalPath,
              fileType: 'pdf',
              fail: function(res) {
                console.log(res)
                util.showToast("打开失败")
              },
              complete: function({
                result
              }) {
                wx.getFileSystemManager().getFileInfo({
                  filePath: ticketLocalPath,
                  success: function(res) {
                    console.log(res)
                  },
                })
                wx.hideLoading()
              }
            })
          },
          fail: function(res) {
            console.log(res)
            util.showToast("下载失败")
            wx.hideLoading()
          }
        })
      },
      fail: function(res) {
        console.log(res)
        util.showToast("下载失败")
        wx.hideLoading()
      }
    })
2 回复

直接用下载好的 tempFilePath,用 openDocument 打开可以吗

你好,看看是否重复调用了该接口?

麻烦提供出现问题的具体机型、微信版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

回到顶部