最近有客户反馈云盘本地上传大文件会报错,上传不了,经过我们排查修复,下面是解决方案:
1. 修改文件路径:src\utils\uploadChunk.js,也可以直接下载附件替换一下文件。
2. 修改代码:
// 针对每个文件进行chunk处理
const readChunkMD5 = async (file, md5, options) => {
const CHUNK_SIZE = 5 * 1024 * 1024 // 5MB
const MAX_RETRIES = 3 // 最大重试次数
const RETRY_DELAY = 1000 // 重试延迟(ms)
const chunkCount = Math.ceil(file.size / CHUNK_SIZE)
const uploadedChunks = [] // 跟踪已上传的分片
let lastProgress = 0 // 跟踪上次进度
// 进度更新函数
const updateProgress = (current) => {
const progress = Math.round((current / chunkCount) * 100)
if (progress > lastProgress && options?.onProgress) {
options.onProgress(progress)
lastProgress = progress
}
}
for (let i = 0; i < chunkCount; i++) {
let retries = 0
let success = false
while (retries < MAX_RETRIES && !success) {
try {
const { chunk } = getChunkInfo(file, i, CHUNK_SIZE)
await uploadChunk(
{
chunk,
currentChunk: i,
chunkCount,
md5
},
options
)
uploadedChunks.push(i)
success = true
updateProgress(i + 1) // 更新进度
} catch (error) {
retries++
if (retries >= MAX_RETRIES) {
throw new Error(`分片 ${i} 上传失败: ${error.message} (${MAX_RETRIES}次重试后)`)
}
// 指数退避重试
const delay = RETRY_DELAY * Math.pow(2, retries)
await new Promise((resolve) => setTimeout(resolve, delay))
}
}
}
return uploadedChunks // 返回成功上传的分片索引
}