微信小程序中pdf的上传和下载

微信小程序中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 });
        },
      });
},

上传2:

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

//pdf上传--原生组件
uploadFileTap:function(e){
	let that = this;
	let myId = that.data.myId; //排单ID
	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
	            })
	        }
	      })
	    }
	  })
},

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.queryPdfUrl, {
       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) {}
},

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