wxml

js


监听图片下载进度
downImg:function(e){
var _this = this
// 获取图片地址
var img = e.currentTarget.dataset.src
console.log(img)
// 下载监听进度
const downloadTask = wx.downloadFile({
url: img,
success:function(res){
console.log(res)
if(res.statusCode === 200){
wx.saveImageToPhotosAlbum({
filePath:res.tempFilePath,
success:function(res){
wx.showToast({
title: '保存图片成功',
})
},
fail:function(res){
wx.showToast({
title: '保存图片失败',
})
}
})
}
}
})
downloadTask.onProgressUpdate((res)=>{
console.log('下载进度', res.progress)
console.log('已经下载的数据长度', res.totalBytesWritten)
console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
if(res.progress === 100){
this.setData({
progress:''
})
}else{
this.setData({
progress:res.progress + '%'
})
}
})
},参考:https://zhuanlan.zhihu.com/p/237325032
https://zhuanlan.zhihu.com/p/41710340