1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| // import {createChunk} from './newcar.js'
function createChunk(file, index, chunkSize) { return new Promise((resolve)=>{ //本次开始和结束切片从XX MB - XX MB const start = index * chunkSize const end = start + chunkSize
const fileReader = new FileReader() //本次切片的BLOB const blob = file.slice(start,end) fileReader.onload = function(e) { resolve({ start, end, index, blob }) }
fileReader.readAsArrayBuffer(blob)
}) }
onmessage = async (e) => { const { file, CHUNK_SIZE, startChunkIndex, endChunkIndex } = e.data
console.log({ file, CHUNK_SIZE, startChunkIndex, endChunkIndex })
const proms = []
for(let i=startChunkIndex; i < endChunkIndex; i++) { proms.push(createChunk(file,i,CHUNK_SIZE)) } const chunks = await Promise.all(proms)
postMessage(chunks)
}
|