小程序在企微小程序上传文件、查看文件

上传文件

// 上传文件
  uploadFile(e) {
    let index  = e.currentTarget.dataset.index
    if (index) {
      if (this.data.fileTwo) {
        wx.showToast({
          title: '只能上传一个PDF文件',
          icon:'none',
          duration: 1500
        })
        return
      }
    }else {
      if (this.data.fileOne) {
        wx.showToast({
          title: '只能上传一个PDF文件',
          icon:'none',
          duration: 1500
        })
        return
      }
    }
    wx.qy.checkSession({
      success: (res) => {
        console.log(res);
        //session_key 未过期,并且在本生命周期一直有效
        // this.chooseMessageFile(index)
      },
      fail: (err) => {
        console.log(err);
        // checkSession().then(ress => {
        //   console.log('重新获取',ress);
        //   this.chooseMessageFile(index)
        // })
        // session_key 已经失效,需要重新执行登录流程
      },
      complete: com => {
        // 1、session 问题 要刷新
        // 2、session没问题,但是出行跨企业问题: 1、企业未安装小程序,需安装 2、session不匹配,需要重新获取一下session
        refreshSession().then(ress => {
          console.log('重新获取',ress);
          this.chooseMessageFile(index)
        })
      }
    })
  
  },

  chooseMessageFile(index) {
    wx.qy.chooseMessageFile({
      count: 1,
      type: 'file',
      success: res => {
        console.log('成功',res);
        let size = res.tempFiles[0].size
        let fileName = res.tempFiles[0].name;
        if (size > (50 * 1024 * 1024) || fileName.indexOf(".pdf") == -1) {
          wx.showToast({
            title: fileName + '文件超出50MB或者不是pdf格式的文件',
            icon:'none',
            duration: 2500
          })
          return
        }
        console.log('路径',res.tempFiles[0].path);
        let src = res.tempFiles[0].path
        upload(src, 3).then(ress => {
          if (index) {
            this.setData({
              fileTwo: ress.data,
              fileTwoName: fileName,
            })
          }else {
            this.setData({
              fileOne: ress.data,
              fileOneName: fileName,
            })
          }
          console.log('pdf链接', ress);
        })
      },
      complete: err => {
        console.log('是否调用成功',err);
      }
    })
  },

查看文件

// 查看pdf
  lookPDF() {
    wx.downloadFile({
      url: this.data.deleteIndex ? this.data.fileTwo : this.data.fileOne, //要打开的文件路径, 
      success: ress => {
        console.log('下载成功',ress);
        wx.openDocument({
          filePath: ress.tempFilePath,
          fileType: 'pdf',
          success: function (res) {
            console.log('打开PDF成功');
            
          },
          complete: (err) => {
            console.log(err);
            this.setData({
              isOperateShow: false,
            })
          }
        })
      },
      fail: err => {
        console.log('下载失败', err);
      }
     
    })
  },

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值