当前位置:   article > 正文

微信小程序中pdf的上传、下载及excel导出_微信小程序上传pdf

微信小程序上传pdf

微信小程序中pdf的上传、下载及excel导出

pdf上传

上传两种方法:

上传1:

1.用vant weapp组件:

//pdf上传--vant weapp组件
<view class="content">
    <van-uploader
     file-list="{{ fileList }}"  
     bind:after-read="afterReadFile" 
     accept="file"  
     upload-icon="plus"
     preview-size="30px"
     max-count="1"
     deletable="{{deletableFile}}"
    >
    </van-uploader>
</view> 

page({
  data:{
  	fileList:[],//pdf上传
  }
})
                
afterReadFile: function (e) {
    let that = this;
    const { file } = e.detail;
    let myId = that.data.myId; //
  
    console.log(file,1000);
    wx.uploadFile({
        url: api.hhh+'?file='+file.url+'&schedulingId='+myId,//服务器上的pdf地址
        filePath: file.url,
        name: 'file',
        // formData: { 
        //     file: file.url,
        //     schedulingId:myId
        // },
        success(res) {
          // 上传完成需要更新 fileList
          const { fileList = [] } = that.data;
          fileList.push({ ...file });
          that.setData({ fileList });
        },
      });
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

上传2:

<view class="content" bindtap="uploadFileTap">
    上传
</view>

//pdf上传--原生组件
uploadFileTap:function(e){
	let that = this;
	let myId = that.data.myId; 
	wx.chooseMessageFile({
	    count: 1,
	    type: 'file',
	    success (res) {
	      // tempFilePath可以作为img标签的src属性显示图片
	      const tempFilePaths = res.tempFiles
	      wx.uploadFile({
	        url: url,//服务器上的pdf地址 
	        filePath: tempFilePaths[0].path,
	        name: 'file',
	        formData: {
	            file: tempFilePaths[0].path,
	            schedulingId:myId
	        },
	        success (res){
	          const data = res.data
	          //do something
	          wx.showToast({
	            title: '数据上传成功!',
	            icon: 'none',
	            duration: 3000
	            })
	        }
	      })
	    }
	  })
},

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

pdf下载

参考:https://blog.csdn.net/weixin_38566069/article/details/110229404

//下载pdf
downloadPDF:function(e){
   let that = this;
   let myId = that.data.myId; 

   wx.showLoading({
       title: '加载中...',
       mask: true
   });
   //获取pdf地址
   app.get(api.xxxx, {
       schedulingId: myId, //
   }).then(res => {
       if (res.code == 200) {
           wx.hideLoading()
           let pdfUrl=res.data ? (res.data.pdfUrl ? res.data.pdfUrl : null) : null
           if(pdfUrl){
               const fileExtName = ".pdf";
               const randfile = new Date().getTime() + fileExtName;
               const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
               that.deletContract();
               //下载
               wx.downloadFile({
                   url: pdfUrl,
                   filePath: newPath,
                   success: function (res) {
                   const filePath = res.tempFilePath;
                       wx.openDocument({
                       filePath: newPath,
                       showMenu: true,
                       fileType: 'pdf',
                       success: function (res) {}
                   })
                   },
                   fail: function (res) {
                   wx.hideLoading();
                   }
               })
           }else{
               wx.showToast({
                   title: '请先上传pdf数据!',
                   icon: 'none',
                   duration: 3000
               })
           }

       } else {
           wx.hideLoading()
           wx.showToast({
               title: '获取数据失败!',
               icon: 'none',
               duration: 3000
           })
       }
   }).catch((err) => {
       wx.hideLoading()
       wx.showToast({
           title: 'err',
           icon: 'none',
           duration: 3000
       })
   });
},

// 删除本地文件
deletContract() {
   try {
   let file = wx.getFileSystemManager();
   file.readdir({
       dirPath: `${wx.env.USER_DATA_PATH}`,
       success: res => {
           console.log(res);
           if (res.files.length > 2) {
               file.unlink({
               filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
               complete: res => {}
               })
           }
       }
   })
   } catch (error) {}
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

导出excel

//导出Excel
  downloadExcel:function(e){
      let that = this;
      let myId = that.data.myId; 

      wx.showLoading({
          title: '加载中...',
          mask: true
      });
      app.get(api.xxxExcel, {
          schedulingId: myId, 
      }).then(res => {
          if (res.code == 200) {
              let excelUrl=res.data ? res.data : null
              if(excelUrl){
                  let type=excelUrl.split('.').pop();
                  let fileExtName = "";
                  if(type==''){//空
                      wx.showToast({
                          title: '导入的非Excel文件!',
                          icon: 'none',
                          duration: 3000
                      })
                      return false
                  }else if(type=='xls'){
                      fileExtName=".xls"
                  }else if(type=='xlsx'){
                      fileExtName=".xlsx"
                  }

                  const randfile = new Date().getTime() + fileExtName;
                  const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
                  that.deletContract();
                  wx.downloadFile({
                      url: excelUrl,
                      filePath: newPath,
                      success: function (res) {
                          wx.hideLoading()
                          const filePath = res.filePath;
                          wx.openDocument({
                              filePath: newPath,
                              showMenu: true,
                              fileType: type,
                              success: function (res) {}
                          })
                      },
                      fail: function (res) {
                          wx.hideLoading();
                      }
                  })
              }else{
                  wx.showToast({
                      title: '请先上导入Excel数据!',
                      icon: 'none',
                      duration: 3000
                  })
              }
        

          } else {
              wx.hideLoading()
              wx.showToast({
                  title: '获取Excel数据失败!',
                  icon: 'none',
                  duration: 3000
              })
          }
      }).catch((err) => {
          wx.hideLoading()
          wx.showToast({
              title: 'err',
              icon: 'none',
              duration: 3000
          })
      });
  },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

提示:突然冒出一个报错:wx.chooseMessageFile点击很多次后就突然无效了
昨天上传功能在【微信开发工具和移动端】都可以用,早上突然实现了。
查了下是官方给出的解释是:
2023年9月15日之前,此功能逻辑只对开发版/体验版生效,开发者请尽快进行隐私弹窗适配、发版。2023年9月15日之后,将对正式版生效,详情可见《关于小程序隐私保护指引设置的公告》。

具体见:
https://developers.weixin.qq.com/community/develop/doc/0002aa86b6ccb056ff20a04e96bc00?jumpto=comment

https://developers.weixin.qq.com/community/develop/doc/00042e3ef54940ce8520e38db61801

导出excel2: 以xlsx库形式

微信小程序利用第三方库xlsx导出excel
参考:
https://blog.csdn.net/weixin_57263939/article/details/128905216
https://blog.csdn.net/gao511147456/article/details/132009965

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/830552
推荐阅读
相关标签
  

闽ICP备14008679号