jiate

微信小程序点击下载按钮,下载多图到手机

视图代码 index.wxml

<button bindtap="baocunImgs">下载保存到手机</button>

 

index.js代码

Page({
 data: {
  
 },
 onLoad: function(options) {

 },
 // 点击下载
baocunImgs() {
 
  var imglist=[
    \'https://s1.ax1x.com/2020/06/02/tY3JmQ.jpg\',
    \'https://s1.ax1x.com/2020/06/02/tY38Og.jpg\'
   ]

  var that=this
    wx.getSetting({
      success(res) {
      if (!res.authSetting[\'scope.writePhotosAlbum\']) {
      wx.authorize({
      scope:\'scope.writePhotosAlbum\',
       success(res) {
          imglist.forEach(function (item, index) {
              that.downloadImage(item)    
          })
       }
      })
      }else{

        imglist.forEach(function (item, index) {
          console.log(item)
              that.downloadImage(item)    
          })
        }
      }
    })
},
   // 下载文件  
downloadImage: function(imageUrl) {  
    wx.downloadFile({  
        url: imageUrl,  
        success: function (res) {  
          console.log("下载文件:success");  
          console.log(res);  
      
          // 保存图片到系统相册  
          wx.saveImageToPhotosAlbum({  
            filePath: res.tempFilePath,  
            success(res) {  
              console.log("保存图片:success");  
              wx.showToast({  
                title: \'保存成功\',  
              });  
            },  
            fail(res) {  
              console.log("保存图片:fail");  
              console.log(res);  
            }  
          })  
        },  
        fail: function (res) {  
          console.log("下载文件:fail");  
          console.log(res);  
        }  
      })  
    }  
 })
 

分类:

技术点:

相关文章:

  • 2022-02-12
  • 2022-01-07
  • 2021-12-06
  • 2021-07-05
  • 2021-06-30
  • 2022-01-07
  • 2021-04-08
猜你喜欢
  • 2022-12-23
  • 2021-12-05
  • 2021-05-17
  • 2021-11-23
  • 2022-12-23
  • 2021-06-09
相关资源
相似解决方案