最近有客戶反饋云盤本地上傳大文件會報錯,上傳不了,經過我們排查修復,下面是解決方案:
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 // 返回成功上傳的分片索引
}