有人可以帮我调试这个功能吗
const chunkify = async (file: Blob) => {
const totalSize = file.size;
const chunkSize = 1024 * 1024 * 100; // 100MB
const chunks = [] as Uint8Array[];
const amountOfChunks = Math.ceil(totalSize / chunkSize);
console.log("Total blob size: ", totalSize);
for (let index = 0; index < amountOfChunks; index++) {
const start = index * chunkSize;
const end = (index + 1) * chunkSize;
console.log("Start point: ", start);
console.log("End point: ", end);
const chunk = await file.slice(start, end).arrayBuffer();
chunks.push(new Uint8Array(chunk));
}
return chunks;
};
作为概述,上面的函数假设通过使用 Android WebView(带有 Ionic 和 Capacitor)提供 0.97 GB 文件,执行大约 7 次(实际上是 6 次,因为在第 7 次迭代时,代码在最后一个 console.log() 处停止在返回下面的错误之前)
Uncaught (in promise) DOMException: The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.
PS:我试着问巴德这个问题,但它建议更换
const end = (index + 1) * chunkSize;
与
const end = Math.min(totalSize, (index + 1) * chunkSize);
但这似乎没有帮助。
所以,没有想到这一点感觉有点愚蠢,但是当我将
chunkSize
变量减少到 10 MB (1024 * 1024 * 10) 时,它就起作用了。
JavaScript 很奇怪