技术 · 思考 · 实践 · 成长

首页/微信小程序/正文

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

微信小程序489 次阅读

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


人各有命,但命运也从来不是随机发牌,而是每个人主动选择的结果。

评论交流

1 条评论 · 支持多级回复
验证码

相关文章

继续阅读