关于小程序:小程序图片下载实用

40次阅读

共计 1320 个字符,预计需要花费 4 分钟才能阅读完成。

以下内容 间接放入即可 亲测无效

单张下载

<!-- HTML -->
<div @click="saveImage"> 下载图片 </div>
/* methods */
saveImage() {
          wx.downloadFile({
              url:'http://upload.jianshu.io/admin_banners/web_images/4435/c1d3ca63353c8bd527f0d781605516cb5b266d02.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540',

              success: function(res) {if (res.statusCode === 200) {
                      let img = res.tempFilePath;
                      wx.saveImageToPhotosAlbum({
                          filePath: img,
                          success(res) {console.log('胜利')
                          },
                          fail(res) {console.log('保留失败')
                          }
                      });
                  }
              }
          });
      }

多张下载

<!-- HTML -->
<div @click="saveImage"> 下载图片 </div>
/* methods */
saveImage() {let picArr=['http://upload.jianshu.io/admin_banners/web_images/4435/c1d3ca63353c8bd527f0d781605516cb5b266d02.jpg','http://47.102.105.248:8805/img-api/202007/24/1595585519996221273.png']
          this.xiazaiTupian(picArr)
      },
      // 下载内容
      xiazaiTupian: function(picArr,index) {
          let that=this;
          index=index||0;
          console.log(picArr[index])
          wx.downloadFile({url: picArr[index],
              success: function (res) {
                  var temp = res.tempFilePath
                  console.log(index)
                  console.log(temp)
                  wx.saveImageToPhotosAlbum({
                      filePath: temp,
                      success: function () {
                          index+=1;
                          if (index < picArr.length){that.xiazaiTupian(picArr, index);
                          }else{
                              wx.showToast({title: '下载实现',})
                          }

                      },
                      fail: function () {
                          index += 1;
                          if (index < picArr.length) {that.xiazaiTupian(picArr, index);
                          } else {
                              wx.showToast({title: '下载实现',})
                          }
                          wx.showToast({title: '第' + (index+1) + '下载失败',
                          })
                      }
                  })
              },
              fail: function (res) {
                  wx.showToast({title: '下载失败',})
              }
          })

      },

正文完
 0