收藏
回答

客户端如何下载并预览云存储的文件?

// 下载并预览文件

downloadFile: function(e) {

    console.log(e);

    let url = e.currentTarget.dataset.url;

    wx.cloud.downloadFile({

      fileID: url

    }).then(res => {

      // get temp file path

      console.log("获取临时链接成功",res.tempFilePath)

      wx.downloadFile({

        url: res.tempFilePath,

        header: {},

        success: function(res) {

            var filePath = res.tempFilePath;

            console.log(filePath);

            wx.openDocument({

                filePath: filePath,

                success: function(res) {

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

                },

                fail: function(res) {

                    console.log(res);

                },

                complete: function(res) {

                    console.log(res);

                }

            })

        },

        fail: function(res) {

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

        },

        complete: function(res) {},

    }) 

    }).catch(error => {

      // handle error

    })

  }

图一:在开发者工具返回http://的临时链接,文档能正常打开:

图二:在手机端(客户端)返回wxfile://的临时文件,文档无法正常打开:

问题:想在小程序中预览云存储的Word文档,在开发者工具是可以正常预览文件的,但在客户端就无法打开。请问这是什么原因,有没有较好的途径解决这个临时链接的问题?

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

2 个回答

  • Mr.Zhao
    Mr.Zhao
    2020-04-29

    下载一遍就行了,wx.download多余了。实测过的

    wx.cloud.downloadFile({
     fileID'cloud://test.docx',
     successres => {
      console.log(res.tempFilePath)
      wx.openDocument({
       filePath: res.tempFilePath,
      })
     },
     failerr => {
      // handle error
     }
    })
    


    2020-04-29
    有用 1
    回复 2
    • 404
      404
      2020-04-29
      可以了,谢谢大佬
      2020-04-29
      回复
    • 404
      404
      2020-04-29
      // 下载并预览文件
        downloadFile: function(e) {
          console.log(e);
          let url = e.currentTarget.dataset.url;
          wx.cloud.downloadFile({
            fileID: url
          }).then(res=>{
            console.log("获取临时链接成功",res.tempFilePath)
            var filePath = res.tempFilePath;
            console.log(filePath);
            wx.openDocument({
                filePath: filePath,
                success: function(res) {
                    console.log('打开文档成功')
                },
                fail: function(res) {
                    console.log('打开文档失败',res);
                },
                complete: function(res) {
                    console.log(res);
                }
            })
          })
        }
      2020-04-29
      回复
  • 🐼
    🐼
    2023-08-15

    哥,为什么我用你的代码获取不到云存储里面的临时链接呢?

    2023-08-15
    有用
    回复
登录 后发表内容
问题标签