计算机网络/计算机科学与应用/系统/运维/开发

微信小程序 下载图片保存到相册

wxml

image.png

js

image.png

image.png


监听图片下载进度

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


人生于世,就得有点兴致,闲暇之时,或乐游山水、或尽享食味、或寄情岁时,它让我们的生活变得充盈。

评论

image

57 2024-08-12 09:08:43

小程序下拉加载分页 https://zhuanlan.zhihu.com/p/39590738

回复 删除

^