1.点击按钮下载文件到临时路径,然后打开文件之后将文件保存到本地路径。
2.点击返回,再次点击按钮,它又会重复第一次的步骤。为什么不是直接打开保存到本地路径的文件。
我的解决思路: 判断文件否已经保存到了本地(保存的本地路径是否在一个变量数组里),如果没有保存过,就执行第一个步骤。如果保存过了,就直接打开(wx.openDocument的filePath能写文件保存本地的路径吗?)
//下载文件
loadFile() {
wx.showLoading({
title: '加载中...',
})
wx.downloadFile({
url: 'http://www.cztouch.com/upfiles/soft/testpdf.pdf',
success: function (res) {
console.log("下载文件成功" + res)
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
showMenu:"true", // 是否显示右上角菜单
success: function (res) {
console.log('打开文档成功')
wx.hideLoading();
},
fail: function(res) {
console.log(res);
},
complete: function(res) {
wx.saveFile({
tempFilePath: filePath,
success (res) {
const savedFilePath = res.savedFilePath
console.log("本地保存路径:" + savedFilePath)
}
})
}
})
},
fail: function(res) {
console.log('文件下载失败');
},
complete: function(res) {
},
})
},
可以先试一下将 res.tempFilePath 缓存下来,下次再取这个路径!
https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html