分享写法
wx.downloadFile({
url: that.pdfUrl,
success(res) {
wx.shareFileMessage({
filePath: res.tempFilePath,
fileName: `${fileName}.${fileType}`,// 可以自定义文件名字,也可以不写
success(res) {},
fail() {
that.showToast('分享失败')
},
complete(res) {
console.log(res);
uni.hideLoading()
}
})
},
fail() {
that.showToast('分享失败')
},
complete() {
uni.hideLoading()
}
})
产生原因:wx.downloadFile()不指定filePath(下载路径)的时候,下载的文件路径是一个临时路径,微信分享的时候无法识别出来临时文件的文件类型(苹果手机和pc端是可以展示的)所以在下载的时候要指定filePath。此时下载的文件就是存储到的用户本地;不在做为临时文件保存(这里具体看需求,如果文件过大,对于用户机器可能会产生影响)
解决代码
wx.downloadFile({
url: that.pdfUrl,
filePath: `${wx.env.USER_DATA_PATH}/${fileName}.${fileType}`, // 添加用户的下载路径可自定义
success(res) {
wx.shareFileMessage({
filePath: res.filePath,// 注意这里不再是res.tempFilePath ,修改为了下载的真实路径res.filePath
fileName: `${fileName}.${fileType}`,// 可以自定义文件名字,也可以不写
success(res) {},
fail() {
that.showToast('分享失败')
},
complete(res) {
console.log(res);
uni.hideLoading()
}
})
},
fail() {
that.showToast('分享失败')
},
complete() {
uni.hideLoading()
}
})