华为云多文件断点续传

在几天没有写出代码的情况下,一直在纠结这个华为云多文件断点续传的问题,直到昨天才突然来了灵感,害,这脑子真是笨死了。不说了,上代码。

这是上传需要调的方法

file(单个文件)

fileObj(上传的文件列表)

uploadUsingString(file, fileObj) {

      this.fileObjs = fileObj

      let _this = this

      let uploadFile = file

      _this.gridData = this.gridData

      const bucketName = "xxxx";

      //getObsClient()里面封装的ak,sk和server

      var obs = this.getObsClient();

      obs.uploadFile({

        Bucket: bucketName,

        Key: uploadFile.name,

        SourceFile: uploadFile,

        PartSize: 9 * 1024 * 1024,

        TaskNum: 4,

        ProgressCallback: function (transferredAmount, totalAmount, totalSeconds) {

          let progressFile = parseInt(transferredAmount * 100.0 / totalAmount)

          let index = _this.gridData.findIndex(v => v.uid == uploadFile.uid)

//上传的页面要显示大小

          if (uploadFile.size < 1024) {

            _this.size = uploadFile.size.toFixed() + "B";

          } else if (uploadFile.size >= 1024 && uploadFile.size < 1048576) {

            _this.size = (uploadFile.size / 1024).toFixed() + "KB";

          } else if (uploadFile.size >= 1048576 && uploadFile.size < 1073741824) {

            _this.size = (uploadFile.size / 1048576).toFixed() + "MB";

          } else if (uploadFile.size >= 1073741824) {

            _this.size = (uploadFile.size / 1073741824).toFixed() + "GB";

          }

          _this.i = index

          if (index == -1) {

            _this.gridData.push({

              size: _this.size,

              name: uploadFile.name,

              progressFile: progressFile,

              uid: uploadFile.uid,

              cancelNum: 0,

              stopNums: 0

            })

          } else {

            if (_this.gridData[index].progressFile != progressFile) {

              _this.gridData[index].progressFile = progressFile;

            }

          }

          //取消上传

          if (_this.stopNum == 0) {

            // 暂停断点续传任务

            console.log(_this.cancelIndex)

            fileObj[_this.cancelIndex].hook.cancel();

          }

        },

        EventCallback: function (eventType, eventParam, eventResult) {

          // 处理事件响应

        },

        ResumeCallback: function (resumeHook, uploadCheckpoint) {

          console.log(uploadCheckpoint)

          fileObj.findIndex(item => {

            if (item.name == uploadCheckpoint.key) {

              item.hook = resumeHook

              item.cp = uploadCheckpoint

            }

          })

        }

      })

    },

    //继续上传

    stateHua(index) {

      let _this = this

      var obs = this.getObsClient();

      obs.uploadFile({

        UploadCheckpoint: _this.fileObjs[index].cp,

        ProgressCallback: function (transferredAmount, totalAmount, totalSeconds) {

          _this.gridData[index].progressFile = parseInt(transferredAmount * 100.0 / totalAmount)

        },

        EventCallback: function (eventType, eventParam, eventResult) {},

        ResumeCallback: function (resumeHook, uploadCheckpoint) {

          _this.fileObjs.findIndex(item => {

            if (item.name == uploadCheckpoint.key) {

              item.hook = resumeHook;

              item.cp = uploadCheckpoint

            }

          })

        }

      })

    },

    //暂停上传

    cancelHua(index) {

      console.log(index)

      let _this = this

      console.log(_this.fileObjs)

      _this.fileObjs[index].hook.cancel();

    }

  把主要的代码列出来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值