微信小程序中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