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

这篇具有很好参考价值的文章主要介绍了微信小程序中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 });
        },
      });
},

上传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
	            })
	        }
	      })
	    }
	  })
},

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

导出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
          })
      });
  },

提示:突然冒出一个报错: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文章来源地址https://www.toymoban.com/news/detail-659171.html

到了这里,关于微信小程序中pdf的上传、下载及excel导出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【微信小程序】导出Excel文件

    在安卓机上能正常预览文件,ios上出现 “OfficeImportErrorDomain”错误912 。此时文件已经保存到了手机,点击右上角三个点用其他方式打开就能看到正常的文件内容,在ios上预览出现了问题。

    2024年02月14日
    浏览(78)
  • 微信小程序导入导出到excel

    补充在最前面:导入不一定用这么复杂的,比如下面的思路就是,把数据复制到粘贴板,然后从粘贴板上取数据,数据处理完成后,可以导入云数据库,也可以导出到站贴板。下面这段看起来有点乱。主要是处理省市县镇村 5级,把表格数据转换成json数据,一开始用导入exc

    2024年02月09日
    浏览(66)
  • uniapp - 微信小程序平台实现预览 office 文件及保存下载到本地功能,将word/excel/ppt/pdf等文件在小程序内进行预览,用户可以保存和转发给好友进行下载到手机(一键复制运行)

    在uniapp微信小程序开发中,预览文件、下载文件并保存到手机本地功能(支持office全套word/pdf/ppt/excel等),兼容安卓和苹果端非常好用, 本文有2种方案,愿意用哪个就用哪个,都有示例代码和详细说明。

    2024年02月08日
    浏览(166)
  • uniapp 微信小程序导出excel功能实现

    需要用到xlsx.core.min.js的js库,以实现导出excel。 下载链接: SheetJS: https://github.com/SheetJS/sheetjs 找到dist目录下,xlsx.core.min.js文件,将它复制到你项目的/common/js/目录下。 1、导入js库; 2、编辑导出数据。  3、利用js库导出excel。 这样就可以了。

    2024年03月21日
    浏览(41)
  • 微信小程序/H5(UniApp)导入/导出excel文件

    XLSX.JS 链接:https://github.com/SheetJS/sheetjs 下载dist目录下的xlsx.mini.min.js即可(根据开发需求可下载其他版本) mini版本只支持xlsx不支持xls哦,需要支持xls的话要下载完整版。 Uniapp可以将js文件放在@/common/js/目录下 日期 事项 支出 2023-01-01 吃饭 300.00 2023-01-02 喝奶茶 28.00 2023-01-03

    2024年02月09日
    浏览(29)
  • 前端实现微信小程序JSON数据导出Excel表

    最近做微信小程序相关项目需要将数据导出为excel形式,在网上查了许多资料来实现这个功能,以下是我使用的方法,特此记录一下,以便之后使用。 解决方法:使用sheetJS代码插件实现 github地址:https://github.com/SheetJS/sheetjs 下载地址:https://cdn.sheetjs.com/ 代码如下,具体使用

    2024年01月17日
    浏览(31)
  • react-前端excel 文件/PDF文件 导入 --导出,照片上传

    需要了解,new FormData() --上传时,将内容转为文件流                   FormData提供一种表示表单数据的键值对的构造方式,实现表单数据的序列化,从而减少表单元素的拼接,提高工作效率                  new FileReader()--base64压缩,目前不了解 一、excel文件导出      三

    2023年04月08日
    浏览(32)
  • 微信小程序查看word,excel,ppt以及pdf文件(文档)

     博主介绍: 本人专注于Android/java/数据库/微信小程序技术领域的开发,以及有好几年的计算机毕业设计方面的实战开发经验和技术积累;尤其是在安卓(Android)的app的开发和微信小程序的开发,很是熟悉和了解;本人也是多年的Android开发人员;希望我发布的此篇文件可以帮

    2024年02月07日
    浏览(152)
  • 微信小程序将接口返回的文件流预览导出Excel文件并转发

    把接口url替换就可以用了 效果

    2024年02月15日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包