Splitting a File into Chunks with Javascript

后端 未结 2 474
花落未央
花落未央 2020-12-10 04:51

I\'m trying to take a single file object and split it into chunks by a specified chunk size. In my example, trying to split a single file into 1MB chunks. So I figure out ho

相关标签:
2条回答
  • 2020-12-10 05:33

    Was slicing off the wrong ends:

    console.log(file.slice(offset,chunkSize));
    

    should have been

    console.log(file.slice(offset,offset+chunkSize));
    
    0 讨论(0)
  • 2020-12-10 05:44

    Use The function below to split a big file into multiple chunks. I have used it with react, it's works.

      createChunks = (file,cSize/* cSize should be byte 1024*1 = 1KB */) => {
       let startPointer = 0;
       let endPointer = file.size;
       let chunks = [];
       while(startPointer<endPointer){
        let newStartPointer = startPointer+cSize;
        chunks.push(file.slice(startPointer,newStartPointer));
        startPointer = newStartPointer;
       }
       return chunks;
      }
    
    0 讨论(0)
提交回复
热议问题