云开发文件下载之后转发出现的问题

1、需要将云存储中的文件如:pdf/word/ppt/等 转发到微信群或好友

2、思路:云储存下载该文件并转发,同时显示下载进度条

share(url) {
    const downloadTask = wx.cloud.downloadFile({
      fileID: url,
      success: res => {
        let tempFilePath = res.tempFilePath
        wx.hideLoading()
         wx.shareFileMessage({
          filePath: tempFilePath,
           success(data) {
           wx.showToast({
              title: '转发成功',
              icon:'none'
            })
          },
           fileName: title,
           fail: e => {
             wx.showToast({
               title: '取消发送',
               icon:'none'
             })
           },
         })
         wx.showModal({
          title: '分享到',
          content: '文件下载完成,请选择要分享的对象',
          complete: (res) => {
           if (res.cancel) {
             wx.showToast({
                 title: '分享取消',
             })
             }
            if (res.confirm) {
              wx.shareFileMessage({
                filePath: tempFilePath,
                success(data) {
                  wx.showToast({
                    title: '转发成功',
                     icon:'none'
                 })
               },
               fileName: title,
                fail: e => {
                  wx.showToast({
                    title: '取消发送',
                    icon:'none'
                  })
                },
              })
            }
          }
        })
       },
       fail: e => {
        wx.hideLoading()
        wx.showToast({
           title: '加载失败,请重试',
          icon:'none'
         })
      }
    })
    //下载进度条
    downloadTask.onProgressUpdate((res) => {
      if (res.progress === 100) {
        return
      } else {
        wx.showLoading({
          title: `正在下载...${res.progress}%`,
        })
      }
    })
    wx.hideLoading()
 }

出现的问题:
(1)开发工具不能实现测试功能;必须要真机调试;
(2)转发的文件是以 wxfile:// 开头,接收到的文件是没办法打开的;

2023.11.05 记录!

3、转变思路

文件下载后先打开预览,然后在右上角三点点击转发!

downloadAndShare(url) {
    const downloadTask = wx.cloud.downloadFile({
      fileID: url,
      success: res => {
        let tempFilePath = res.tempFilePath
        wx.hideLoading()
        //打开文件,预览之后即可转发
        wx.openDocument({
          filePath: tempFilePath,
          showMenu:true
        })
      }
    })

    
    //下载进度条
    downloadTask.onProgressUpdate((res) => {
      if (res.progress === 100) {
        return
      } else {
        wx.showLoading({
          title: `正在下载...${res.progress}%`,
        })
      }
    })
    wx.hideLoading()
  },

当然,为了减少用户的不断下载,可以设置缓存检查!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值