当前位置: 首页 > news >正文

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

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

  • pdf上传
    • 上传1:
    • 上传2:
  • pdf下载
  • 导出excel

pdf上传

上传两种方法:

上传1:

1.用vant weapp组件:

//pdf上传--vant weapp组件
<view class="content"><van-uploaderfile-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) {// 上传完成需要更新 fileListconst { 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.tempFileswx.uploadFile({url: url,//服务器上的pdf地址 filePath: tempFilePaths[0].path,name: 'file',formData: {file: tempFilePaths[0].path,schedulingId:myId},success (res){const data = res.data//do somethingwx.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) : nullif(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

//导出ExceldownloadExcel: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 : nullif(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

http://www.lryc.cn/news/130395.html

相关文章:

  • Python_11 类的方法
  • CentOS系统环境搭建(一)——Centos7更新
  • Mariadb高可用MHA
  • SASS 学习笔记 II
  • 提高 Snowflake 工作效率的 6 大工具
  • 选项方式读取配置IOption、IOptionSnapshot、IOpstionMonitor的区别
  • linux基础面试题整理
  • IDEA开发项目时一直出现http404错误的解决方法
  • NLPR、SenseTime 和 NTU 加速自动视频纵向编辑
  • layui下拉框select 弹出层在最外层
  • fnn手动实现和nn实现(包括3种激活函数、隐藏层)
  • Lua + mysql 实战代码
  • 智慧工地监管云平台源码 建筑施工一体化信息管理系统源码
  • 三.net core 自动化发布到docker (创建一个dotnet工程发布)
  • 【Spring Cloud 八】Spring Cloud Gateway网关
  • Android JNI传递CallBack接口并接收回调
  • 机器学习:特征工程之特征预处理
  • 高级艺术二维码制作教程
  • 每日一题leetcode--使循环数组所有元素相等的最少秒数
  • tauri-react:快速开发跨平台软件的架子,支持自定义头部UI拖拽移动和窗口阴影效果
  • k8s 自身原理之 Service
  • arduino Xiao ESP32C3 oled0.96 下雪花
  • ElasticSearch索引库、文档、RestClient操作
  • Effective Java 案例分享(九)
  • SpringBoot复习:(56)使用@Transactional注解标记的方法的执行流程
  • JVM——引言+JVM内存结构
  • open cv学习 (十)图形检测
  • 【C语言】字符函数和字符串函数
  • 前馈神经网络正则化例子
  • spring的核心技术---bean的生命周期加案例分析详细易懂