微信小程序下载官方链接
developers.weixin.qq.com/miniprogram…
需要配置下载对应的域名
核心代码
// ios端需要指定下载的文件类型
downloadFile (url, type='') {
wx.showLoading({
title: '下载中',
})
let downloadTask = wx.downloadFile({
url: url,
success: function (e) {
wx.hideLoading()
wx.showToast({
title: '下载完成',
})
let tempFilePath = e.tempFilePath
wx.openDocument({
filePath: tempFilePath,
fileType: type,
showMenu: true,
complete: function (e) {
console.log(e)
}
})
}
})
downloadTask.onProgressUpdate((res) => {
wx.showToast({
title: '下载进度:' + res.progress,
})
// console.log('已经下载的数据长度', res.totalBytesWritten)
// console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
})
},
下载文件后使用openDocument可以打开文件,用户也可以自行对文件进行处理(另存为,打开方式等)
onprogressUpdate可以监控下载进度,根据情况做相应的处理。(如加载loading等)
确认好自己的环境和版本,进行线上测试