1. 计算预计上传完成的所需时间

2. 计算平均上传速度
master
huanggy 2 years ago
parent bcf685e538
commit e7f1611477

@ -51,10 +51,22 @@ const getTaskInfo = async (file) => {
*/
const handleUpload = (file, taskRecord, options) => {
let lastUploadedSize = 0; //
let uploadedSize = 0 //
const totalSize = file.size || 0 //
let startMs = new Date().getTime(); //
const { exitPartList, chunkSize, chunkNum, fileIdentifier } = taskRecord
// byte/s
const getSpeed = () => {
// - = byte
const intervalSize = uploadedSize - lastUploadedSize
const nowMs = new Date().getTime()
// s
const intervalTime = (nowMs - startMs) / 1000
return intervalSize / intervalTime
}
const uploadNext = async (partNumber) => {
const start = new Number(chunkSize) * (partNumber - 1)
const end = start + new Number(chunkSize)
@ -65,9 +77,7 @@ const handleUpload = (file, taskRecord, options) => {
url: data,
method: 'PUT',
data: blob,
headers: {
'Content-Type': 'application/octet-stream'
}
headers: {'Content-Type': 'application/octet-stream'}
})
return Promise.resolve({ partNumber: partNumber, uploadedSize: blob.size })
}
@ -90,6 +100,12 @@ const handleUpload = (file, taskRecord, options) => {
const percent = Math.round(uploadedSize / totalSize * 100).toFixed(2);
onProgress({percent: percent})
}
const speed = getSpeed();
const remainingTime = speed != 0 ? Math.ceil((totalSize - uploadedSize) / speed) + 's' : '未知'
console.log('剩余大小:', (totalSize - uploadedSize) / 1024 / 1024, 'mb');
console.log('当前速度:', (speed / 1024 / 1024).toFixed(2), 'mbps');
console.log('预计完成:', remainingTime);
}
@ -109,7 +125,8 @@ const handleUpload = (file, taskRecord, options) => {
for (let partNumber = 1; partNumber <= chunkNum; partNumber++) {
const exitPart = (exitPartList || []).find(exitPart => exitPart.partNumber == partNumber)
if (exitPart) {
//
// ,
lastUploadedSize += new Number(exitPart.size)
updateProcess(exitPart.size)
} else {
queue.push(() => uploadNext(partNumber).then(res => {
@ -185,7 +202,7 @@ const handleRemoveFile = (uploadFile, uploadFiles) => {
drag
action="/"
multiple
:http-request="handleHttpRequest"
:http-request="handleHttpRequest"
:on-remove="handleRemoveFile">
<el-icon class="el-icon--upload"><upload-filled /></el-icon>
<div class="el-upload__text">

Loading…
Cancel
Save