1、需要将云存储中的文件如:pdf/word/ppt/等 转发到微信群或好友
2、思路:云储存下载该文件并转发,同时显示下载进度条
share(url) {
const downloadTask = wx.cloud.downloadFile({
fileID: url,
success: res => {
let tempFilePath = res.tempFilePath
wx.hideLoading()
wx.shareFileMessage({
filePath: tempFilePath,
success(data) {
wx.showToast({
title: '转发成功',
icon:'none'
})
},
fileName: title,
fail: e => {
wx.showToast({
title: '取消发送',
icon:'none'
})
},
})
wx.showModal({
title: '分享到',
content: '文件下载完成,请选择要分享的对象',
complete: (res) => {
if (res.cancel) {
wx.showToast({
title: '分享取消',
})
}
if (res.confirm) {
wx.shareFileMessage({
filePath: tempFilePath,
success(data) {
wx.showToast({
title: '转发成功',
icon:'none'
})
},
fileName: title,
fail: e => {
wx.showToast({
title: '取消发送',
icon:'none'
})
},
})
}
}
})
},
fail: e => {
wx.hideLoading()
wx.showToast({
title: '加载失败,请重试',
icon:'none'
})
}
})
//下载进度条
downloadTask.onProgressUpdate((res) => {
if (res.progress === 100) {
return
} else {
wx.showLoading({
title: `正在下载...${res.progress}%`,
})
}
})
wx.hideLoading()
}
出现的问题:
(1)开发工具不能实现测试功能;必须要真机调试;
(2)转发的文件是以 wxfile:// 开头,接收到的文件是没办法打开的;
2023.11.05 记录!
3、转变思路
文件下载后先打开预览,然后在右上角三点点击转发!
downloadAndShare(url) {
const downloadTask = wx.cloud.downloadFile({
fileID: url,
success: res => {
let tempFilePath = res.tempFilePath
wx.hideLoading()
//打开文件,预览之后即可转发
wx.openDocument({
filePath: tempFilePath,
showMenu:true
})
}
})
//下载进度条
downloadTask.onProgressUpdate((res) => {
if (res.progress === 100) {
return
} else {
wx.showLoading({
title: `正在下载...${res.progress}%`,
})
}
})
wx.hideLoading()
},
当然,为了减少用户的不断下载,可以设置缓存检查!