//并发控制
arr是文件上传的fileList数组
const limitLoad = (arr, load, limit) => {
const sequence = [].concat(arr);
let promise = [];
promise = sequence.splice(0, limit).map((url, index) => {
return load(url).then(() => {
return index;
});
});
let p = Promise.race(promise);
// for循环给p赋值相当于.then().then()链式调用
for (let i = 0; i < sequence.length; i++) {
p = p.then((res) => {
promise[res] = load(sequence[i]).then(() => {
return res;
});
return Promise.race(promise);
});
}
};
const load = (file) => {
return new Promise((reslove, reject) => {
updateFileList(file, reslove);//调用七牛的上传
});
};
//上传文件
const updateFileList = async (file, resolve) => {
//获取视频或者音频时长
var fileurl = URL.createObjectURL(file);
var audioElement = new Audio(fileurl);
audioElement.addEventListener("loadedmetadata", function (_event) {
file.duration = parseInt(audioElement.duration);
});
const config = {
useCdnDomain: true,
region: qiniu.region.z2, // 根据地区不同,官网有不同的配置
};
const key =
"uploads/" +
("video/mp4".includes(file.name.split(".")[1]) ? "video" : "image") +
"/" +
moment().format("YYYY-MM-DD") +
"/" +
Math.floor(Math.random() * 99999) +
"/" +
file.name;
const observable = await qiniu.upload(
file,
key, //文件名
UserQiniu.token,
config
);
// 上传视频文件
observable.subscribe({
next(res) {
//上传进度
ruleForm.fileList.forEach((item) => {
if (item.raw.uid == file.uid) {
item.percentage = res.total.percent;
}
});
},
error(err) {
},
complete(res) {
'''
'''
resolve();
},
});
上传文件的几个回调根据自己的情况设置条件即可 参考意义不大