【微信小程序】简记功能--转发文件给用户(下载文件到用户手机)

需求

转发文件给用户

相关链接

DownloadTask | 微信开放文档

DownloadTask | 微信开放文档

wx.shareFileMessage(Object object) | 微信开放文档

注意点

1、实际操作只能在真机演示;

2、下载文件所属的域名需要在小程序后台进行downloadFile合法域名添加

代码实现

wxml


  
    商务方案.pdf
    转发
  

js 

Page({
  /* 转发 */
  bindQueryShare(e) {
    let that = this
    let dataset = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '是否转发文件',
      success: (res) => {
        // 点击确定
        if (res.confirm) {
          console.log("开始下载")
          let downloadTask = wx.downloadFile({
            url: "https://xxx/template.pdf",
            filePath: wx.env.USER_DATA_PATH + '/' + dataset.filename,
            fileName: dataset.filename,
            success: (res) => {
              // 下载完成,发起文件分享转发
              if (res.statusCode == 200) {
                wx.shareFileMessage({
                  filePath: res.filePath,
                  fileType: dataset.filetype,
                  fileName: dataset.filename,
                  success: (res) => {
                    console.log("转发成功",res)
                  },
                  fail: (res) => {
                    wx.showToast({
                      title: '转发失败',
                    })
                  }
                })
              }
            },
            fail: console.error
          })
          // 监听下载进度
          downloadTask.onProgressUpdate((res) => {
            console.log('监听下载过程', res)
            wx.showLoading({
              title: "进度:" +res.progress+ "%",
            })
            if (res.progress == 100) {
              wx.hideLoading()
            }
          })
        }
        // 点击取消
        if (res.cancel) {}
      }
    })
  }
})

效果截图 

【微信小程序】简记功能--转发文件给用户(下载文件到用户手机)_第1张图片 【微信小程序】简记功能--转发文件给用户(下载文件到用户手机)_第2张图片

你可能感兴趣的:(前端,小程序,微信,微信小程序,小程序,前端)