unicloud35-自定义上传 promise封装批量上传

145 阅读3分钟

uploadfile方法一次只能上传一个,不能批量上传

需要用for循环对保存图片地址的数组进行遍历,每次遍历进行上传,这样实现批量上传文件

把之前写在goupload方法中的上传方法封装到一个新方法uploadFun中,然后goupload这个方法就用来对保存图片临时地址的变量temfiles进行循环,每次循环将遍历出来的结果传递给uploadFun方法作为参数进行上传

    methods:{
      // 点击按钮上传图片
      goUpload(){
        // 把上传方法封装到uploadFun方法中,在这里写批量上传
        this.temFiles.forEach(item=>{
          this.uploadFun(item)
        })
      },
      // 批量上传图片
      uploadFun(item){
        uniCloud.uploadFile({
          filePath:item.path,
          // 这里文件的后缀需要先获取到真实的文件后缀
          cloudPath:item.name
        }).then(res=>{
          console.log(res);
        })
      },

选择两张图片进行上传,成功后,云存储里已经有这两张图片了 image.png

image.png

上传完成后,应该把图片url地址保存到数据库中,需要对图片上传进行监听,用promise方法对上传进行监听

uploadFun方法用了then来接收结果,他就是一个promise方法,不再用then捕获结果了,直接把结果return出去,如果在上面调用uploadFun方法的goupload中,调用then方法,也能拿到结果

      // 点击按钮上传图片
      goUpload(){
        // 把上传方法封装到uploadFun方法中,在这里写批量上传
        this.temFiles.forEach(item=>{
          this.uploadFun(item).then() 
        })
      },
      // 批量上传图片
      uploadFun(item){
        return uniCloud.uploadFile({
          filePath:item.path,
          // 这里文件的后缀需要先获取到真实的文件后缀
          cloudPath:item.name
        })
      },

这里不用上面这种方法,把foreach改成map方法,然后把上传的结果,也就是map遍历的结果return出去,这里map的结果就相当于是把拿到的结果赋值给一个新数组,所以定义一个新变量来接收这个结果

foreach与map的区别:1.相同点,都是循环遍历数组中的每一项;匿名函数中的this都是指向window。2.不同点,map()会分配内存空间存储新数组并返回,forEach()不会返回数据;forEach()允许callback更改原始数组的元素。

    methods:{
      // 点击按钮上传图片
      goUpload(){
        // 把上传方法封装到uploadFun方法中,在这里写批量上传
        // 这是一个异步请求
        let newsArr = this.temFiles.map(async item=>{
          return await this.uploadFun(item)
        })
        // 获取promise请求的状态,then是将异步请求同步化
        // 监听请求是否全部完成,上传全部完成then方法才会执行
        Promise.all(newsArr).then(res=>{
          console.log(res);
        })
      },
      // 批量上传图片
      uploadFun(item){
        uniCloud.uploadFile({
          filePath:item.path,
          // 这里文件的后缀需要先获取到真实的文件后缀
          cloudPath:item.name
        }).then(res=>{
          console.log(res);
        })
      },

image.png

then方法返回值中有一个属性值fileID,就是文件保存在云存储中的url地址,拿到这个地址就表示上传成功了

mp-434fcb27-c15c-4386-8fb4-df14ff635e79.cdn.bspapp.com/cloudstorag…

image.png

将这个url地址保存到数据库

      // 点击按钮上传图片
      goUpload(){
        // 把上传方法封装到uploadFun方法中,在这里写批量上传
        // 这是一个异步请求
        let newsArr = this.temFiles.map(async item=>{
          return await this.uploadFun(item)
        })
        // 获取promise请求的状态,then是将异步请求同步化
        // 监听请求是否全部完成,上传全部完成then方法才会执行
        Promise.all(newsArr).then(res=>{
          // 将返回值item中的fillID,也就是文件的云存储路径遍历出来
          let arr=res.map(item=>{
            return item.fileID
          })
          // 将拿到的图片云存储url保存到变量中
          this.picArr=arr;
          console.log(this.picArr);
        })
      },
      // 批量上传图片
      uploadFun(item){
        uniCloud.uploadFile({
          filePath:item.path,
          // 这里文件的后缀需要先获取到真实的文件后缀
          cloudPath:item.name
        }).then(res=>{
          console.log(res);
        })
      },