微信小程序上实现下载pdf功能
onLookFile() {
let that = this;
const filename = '下载附件'
const fileExtName = ".pdf";
const randfile = filename + fileExtName;
//wx.env.USER_DATA_PATH 是微信提供了一个用户文件目录给开发者,开发者对这个目录有完全自由的读写权限
const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
that.deletContract();
wx.downloadFile({
url: 'xxx', // 后端下载地址
filePath: newPath,
success: function (res) {
const filePath = res.tempFilePath;
console.log(filePath)
wx.openDocument({
filePath: newPath,
showMenu: true,
fileType: 'pdf',
})
},
fail: function () {
wx.hideLoading();
}
})
},
// 删除本地文件
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]}`,
})
}
}
})
} catch (error) { }
},
原文链接:https://blog.csdn.net/LXY_1999/article/details/128012155