1. 程式人生 > 其它 >微信小程式:下載圖片列表

微信小程式:下載圖片列表

技術標籤:小程式小程式

這裡可以下載一張,也可以下載多張圖片,不過是一張一張下載的

// 下載圖片
	 // 點選一鍵下載圖片按鈕
	 DownloadImgList() {
		 var downLoadList=[]
		 this.data.imgList.map(item=>{
			 if(item.selected){
				 downLoadList.push(item.url)
			 }
		 })
    if (downLoadList.length == 0) { // 判斷陣列是否有圖片
      wx.showToast({
        title: '暫無圖片下載',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    // 判斷使用者是否授權
    wx.getSetting({
      success: function (res) {
        if (res.authSetting["scope.writePhotosAlbum"] == false) {
          wx.showToast({
            title: '您還未授權儲存相簿,請授權',
            icon: 'none',
            duration: 2000
          })
          wx.openSetting({
            success: function (res) {
              if (res.authSetting["scope.writePhotosAlbum"] == true) {
                // 儲存相簿
                this.dowImg();
              } 
              if (res.authSetting["scope.writePhotosAlbum"] == false) {
                wx.showToast({
                  title: '授權失dsds敗!',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          });
        } else {
          // 遍歷所有圖片
          for (let i = 0; i < downLoadList.length; i++) {
            wx.downloadFile({
              url: downLoadList[i], // 需下載的每一張圖片路徑
              success: function (res) {
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function (response) {
                    if (response.errMsg == "saveImageToPhotosAlbum:ok") {
                      wx.showToast({
                        title: '下載成功',
                        icon: 'success',
                        duration: 2000
                      })
                    }
                  }
                });
              },
              fail: function () {
                wx.showToast({
                  title: '下載失敗',
                  icon: 'none',
                  duration: 2000
                })
              }
            });
          }
        }
      }
    });
  },

我忘記是從哪位博主哪裡複製來的了,如果哪位博主看到,私信我我改為轉載