钉钉小程序上传预览下载word,pdf文档等一系列问题

钉钉小程序上传预览下载word,pdf文档等一系列问题

小程序”用完即走“的理念使得有着很好的发展市场,不仅微信小程序,支付宝,钉钉小程序等都受用户的喜爱。
刚接触了一个企业内部应用的钉钉小程序,又一个上传word,pdf等文件,并且需要预览和下载,经过对文档的深入琢磨,有了以下的处理方法。
1、上传附件到钉盘/从钉盘选择文件
上传文件需要获取企业自定义钉盘空间,也需要授权上传

//上传文件
  onFile(){
    var that=this;
    app.post('/user/space/index',{
      }).then(res => {
        console.log(res,'上传文件')
        if(res.data.code==0){ 
          var space = that.data.space_id;  //首先要获取企业自定义钉盘空间
          dd.uploadAttachmentToDingTalk({
            image:{multiple:true,compress:false,max:9,spaceId:space},
            space:{spaceId:space,isCopy:1,max:9},
            file: {spaceId:space,max:9},
            types:["photo","camera","file","space"],
            success: (res) => {
              console.log(res)
              var fileData=res.data;
              fileData=fileData.concat(that.data.fileData);
              that.setData({
                  fileData:fileData
                });
            },
            fail: (err) =>{
              dd.alert({
                  content:JSON.stringify(err)
              })
            }
          })
        }else if(res.data.code==1){
           dd.alert({
              title: '未授权,请联系管理员授权',
          });
        }
        
      }) 
  },

2、钉盘文件预览

钉盘预览也需要获取到企业自定义钉盘空间,传入spaceId,并且需要授权下载,传入需要下载的文件id列表

//预览文件
  onFileIpt(event){
    var file=event.target.dataset.attr;
    app.post('/user/space/index',{  //获取企业自定义钉盘空间
      }).then(res => {
        console.log(res)
        if(res.data.code==0){
           app.post('/user/download/index',{ //授权下载权限
             file_id:file.fileId
            }).then(res => {
              if(res.data.code==0){
                dd.previewFileInDingTalk({  //预览
                    spaceId:file.spaceId,
                    fileId:file.fileId,
                    fileName:file.fileName,
                    fileSize:file.fileSize,
                    fileType:file.fileType,
                      success: (res) => {
                      console.log(res)

                    },
                    fail: (err) =>{
                      console.log(err)
                      
                    }
                })
              }else{
                dd.alert({
                    title: '下载未授权,请联系管理员授权',
                });
              }
            }   
        }else if(res.data.code==1){
           dd.alert({
              title: '未授权,请联系管理员授权',
          });
        }
        
      })  
  },
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
⼩程序下载⽂件并预览 需求分析 最近优化了个原先写过的需求,回头复习复习⼩程序的 API 是这样的,我需要下载⼀个⽂件并打开此⽂件 优化:记录是否被下载过,如果下载过就直接打开 获取⽂件下载链接,打开⽂件 wx.downloadFile({ url: `${pic.meetingUrl}/weixin/noticeStart/downLoad/${this.selectItem.recordId}`, // 下载资源的 url success: (res) => { wx.openDocument({ filePath: res.tempFilePath, // ⽂件路径,可通过 downloadFile 获得, fileType: 'pdf', // 写下载后的⽂件的格式,这⾥距离是pdf success: (res) => { }, fail: (error) => { }, }); }, fail: () => {}, complete: () => {}, }); 优化 思路:保存下载后的⽂件,保存其路径到本地缓存,然后下次打开的时候判断⼀下本地缓存⾥是否有,如果有就打开,本地缓存没有的话, 就先下载在打开。 const that = this; const cacheFilePath = wx.getStorageSync( `filePath-${this.selectItem.recordId}` ); // 先判断这个⽂件是否下载过 // 如果下载过,尝试通过缓存,打开⽂件 // 否则就要下载下载成功后保存本地缓存(临时路径信息),命名规则为 filePath + recordId if (cacheFilePath) { wx.openDocument({ filePath: cacheFilePath, //⽂件路径,可通过 downFile 获得, fileType: this.selectItem.fileType, // 获取⽂件格式 success: (res) => { }, fail: (error) => { }, }); } else { wx.showLoading({ title: "下载中", mask: true, }); wx.downloadFile({ url: `${pic.meetingUrl}/weixin/noticeStart/downLoad/${this.selectItem.recordId}`, // 下载资源的 url success: (res) => { // 隐藏 loading wx.hideLoading(); // 下载成功后,保存⽂件路径到本地缓存 wx.setStorageSync( `filePath-${this.selectItem.recordId}`, `${res.tempFilePath}` ); // 尝试打开下载后的⽂件 wx.openDocument({ filePath: res.tempFilePath, //⽂件路径,可通过 downFile 获得, fileType: this.selectItem.fileType, // 获取⽂件格式 success: (res) => { }, fail: (error) => { }, }); }, fail: () => {}, complete: () => {}, }); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值