在几天没有写出代码的情况下,一直在纠结这个华为云多文件断点续传的问题,直到昨天才突然来了灵感,害,这脑子真是笨死了。不说了,上代码。
这是上传需要调的方法
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();
}
把主要的代码列出来了