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: '數(shù)據(jù)上傳成功!',
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數(shù)據(jù)!',
icon: 'none',
duration: 3000
})
}
} else {
wx.hideLoading()
wx.showToast({
title: '獲取數(shù)據(jù)失敗!',
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數(shù)據(jù)!',
icon: 'none',
duration: 3000
})
}
} else {
wx.hideLoading()
wx.showToast({
title: '獲取Excel數(shù)據(jù)失??!',
icon: 'none',
duration: 3000
})
}
}).catch((err) => {
wx.hideLoading()
wx.showToast({
title: 'err',
icon: 'none',
duration: 3000
})
});
},
提示:突然冒出一個報錯:wx.chooseMessageFile點擊很多次后就突然無效了
昨天上傳功能在【微信開發(fā)工具和移動端】都可以用,早上突然實現(xiàn)了。
查了下是官方給出的解釋是:
2023年9月15日之前,此功能邏輯只對開發(fā)版/體驗版生效,開發(fā)者請盡快進行隱私彈窗適配、發(fā)版。2023年9月15日之后,將對正式版生效,詳情可見《關(guān)于小程序隱私保護指引設(shè)置的公告》。
具體見:
https://developers.weixin.qq.com/community/develop/doc/0002aa86b6ccb056ff20a04e96bc00?jumpto=comment文章來源:http://www.zghlxwxcb.cn/news/detail-659171.html
https://developers.weixin.qq.com/community/develop/doc/00042e3ef54940ce8520e38db61801文章來源地址http://www.zghlxwxcb.cn/news/detail-659171.html
到了這里,關(guān)于微信小程序中pdf的上傳、下載及excel導出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!