小程序上传多张图片工具方法
发布于 3 年前 作者 li24 4032 次浏览 来自 分享

//上传图片

const upLoadFileList=(fileList,callBack)=>{

  if(fileList.length==0){

    typeof callBack == “function” && callBack([])

    return;

  }

  wx.showLoading({

    title: ‘上传中’,

  });

  const upImgList=[];

  const length=fileList.length;

  const timestamp = Date.parse(new Date());

  for (let i = 0; i < length; i++) {

    const filePath = fileList[i];

    const cloudPath = `upLoadFiles/images/${timestamp+i+filePath.match(/\.[^.]+?$/)[0]}`;

    wx.cloud.uploadFile({

      cloudPath,

      filePath,

      success: res => {

        upImgList.push(res.fileID);

        if(upImgList.length==length){

          typeof callBack == “function” && callBack(upImgList)

        }

      },

      fail:res=>{

        wx.showToast({

          icon: ‘none’,

          title: ‘上传失败’,

        });

        typeof callBack == “function” && callBack([]);

        return;

      }

     })

  }

}

module.exports = {

  upLoadFileList

}

1 回复

我的个人博客记录也曾写过类似,欢迎交流

回到顶部