收藏
回答

如何将从云存储返回的图片路径合成一个数组?

回答关注问题邀请回答
收藏

4 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2020-05-11

    1、使用Promise.all

    //选择产品图片
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success : (res)=>{
        console.log(res)
        const tempFilePaths = res.tempFilePaths
        this.setData({
          imagesgoods:tempFilePaths
        })
        var ps = [], len=res.tempFilePaths.length
        for(var i in tempFilePaths){
          ps.push((function(f){
            return new Promise((rs, rj)=>{
              //上传产品图片到云存诸
              wx.cloud.uploadFile({
                cloudPath: 'goodsimages/'+new Date().getTime()+'.png',
                filePath: f, // 文件路径
              }).then(res => {
                rs(res.fileID)
              }).catch(error => {
                rj(error)
              })
            })
          })(tempFilePaths[i]))
        }
        ps.length && Promise.all(ps).then(res=>{
          // 上传成功的fileID数组,所有都上传成功才会在这里输出
          console.log(res)
        }).catch(error => {
          // 其中有一个或以上失败,都会在这里输出
          console.error(error)
        })
      }
    })
    

    2、使用递归上传

    xxxx: function(){
      var t = this
      t.uploadedFileIDs = []
      //选择产品图片
      wx.chooseImage({
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success : (res)=>{
          console.log(res)
          const tempFilePaths = res.tempFilePaths
          this.setData({
            imagesgoods:tempFilePaths
          })
          var ps = [], len=res.tempFilePaths.length
          console.log('准备上传文件')
    
    
          t.oneUpload(tempFilePaths, 0)
    
    
          console.log('已上传成功的fileIDs:', t.uploadedFileIDs)
        }
      })
    },
    oneUpload: function(files, idx){
      var t = this, file = files[idx] || '',
      finish = ()=>console.log('上传完毕,上传成功'+t.uploadedFileIDs.length+'张,失败'+(files.length - t.uploadedFileIDs.length)+'张'),
      checkAndUpload = (idx)=>{
        // 判断是否继续上传下一个
        idx+1 <= files.length ? t.oneUpload(files, idx+1) : finish()
      }
      if(!file) {
        checkAndUpload(idx)
      }else{
        console.log('正在上传第'+(idx+1)+'张图片:', file)
        //上传产品图片到云存诸
        wx.cloud.uploadFile({
          cloudPath: 'goodsimages/'+new Date().getTime()+'.png',
          filePath: file, // 文件路径
        }).then(res => {
          console.log('上传第'+(idx+1)+'张图片成功,fileID:', res.fileID)
          // 往uploadedFileIDs里push上传成功返回的fileID
          t.uploadedFileIDs.push(res.fileID)
          checkAndUpload(idx)
        }).catch(error => {
          console.error('上传文件:'+file+'出错,索引:'+idx, error)
          checkAndUpload(idx)
        })
      }
    }
    

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

    2020-05-11
    有用 1
    回复 2
    • 朱_kambo
      朱_kambo
      2020-05-11
      用方法一的效果,请教下如何处理
      2020-05-11
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2020-05-11回复朱_kambo
      我代码没写完,已修改了
      2020-05-11
      回复
  • W
    W
    2020-05-11

    你是不是在uploadFile上面有套循环啊?

    那你先把老的列表保存一下,然后往后追加。

    let newarray = oldarray.push(res.fileId)

    2020-05-11
    有用
    回复 1
    • 朱_kambo
      朱_kambo
      2020-05-11
      帮我看一下如何改?


       //选择产品图片
        wx.chooseImage({
          count: 9,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success : (res)=>{
            console.log(res)
            const tempFilePaths = res.tempFilePaths
            this.setData({
              imagesgoods:tempFilePaths
             })
             var len=res.tempFilePaths.length
            for(var i=0;i<len;i++){
              console.log(i)
           const tempFiles = res.tempFilePaths[i]
           console.log(55555555)
           console.log(tempFiles)
           console.log(66666666666)
            //上传产品图片到云存诸
            wx.cloud.uploadFile({
              cloudPath: 'goodsimages/'+new Date().getTime()+'.png',
              filePath: tempFiles, // 文件路径
            }).then(res => {
              console.log(res.fileID);
              let array=[]
                array.push(res.fileID);
              this.setData({
                goodsimage:array
              })
            }).catch(error => {
              // handle error
            })
          }
          }
        })
      },
      2020-05-11
      回复
  • Admin ²º²³
    Admin ²º²³
    2020-05-10
    let array=[];
    ...
    arrary.push(xxx);
    setData({xxx:array});
    
    2020-05-10
    有用
    回复 3
    • 朱_kambo
      朱_kambo
      2020-05-10
      不知道为什么只push了一个
      2020-05-10
      回复
    • W
      W
      2020-05-10回复朱_kambo
      array.push(...xxx)
      2020-05-10
      回复
    • 朱_kambo
      朱_kambo
      2020-05-10回复W
      变成了这样
      2020-05-10
      回复
  • 拾忆
    拾忆
    发表于小程序端
    2020-05-10
    获取到地址的时候push到data的一个数组里不就可以吗?
    2020-05-10
    有用
    回复 5
    • 朱_kambo
      朱_kambo
      2020-05-10
      怎么定代码??
      2020-05-10
      回复
    • 朱_kambo
      朱_kambo
      2020-05-10
      是这样写吗
      2020-05-10
      回复
    • 拾忆
      拾忆
      发表于移动端
      2020-05-10回复朱_kambo
      是这个意思
      2020-05-10
      回复
    • 朱_kambo
      朱_kambo
      2020-05-10回复拾忆
      不知道为什么只push了一个
      2020-05-10
      回复
    • xplee
      xplee
      2020-05-10回复朱_kambo
      每次成功上传后声明一个array局部变量,然后push进刚刚的fileID,然后this.setData,可不就只有一个。如果你多次调用了upload,请在调用前声明一个array变量,而不是在then内部声明。
      2020-05-10
      回复
登录 后发表内容
问题标签