Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a File into Chunks with Javascript

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 how many chunks it would take, then I'm trying to slice the file starting from the 'offset' (current chunk I'm on * chunk size), and slicing off a chunk size. My first slice comes out properly at 1MB but my subsequent slices turn out to 0, any ideas why? Have a working codepen here:

http://codepen.io/ngalluzzo/pen/VvpYKz?editors=001[1]

var file = $('#uploadFile')[0].files[0];
  var chunkSize = 1024 * 1024;
  var fileSize = file.size;
  var chunks = Math.ceil(file.size/chunkSize,chunkSize);
  var chunk = 0;

  console.log('file size..',fileSize);
  console.log('chunks...',chunks);

  while (chunk <= chunks) {
      var offset = chunk*chunkSize;
      console.log('current chunk..', chunk);
      console.log('offset...', chunk*chunkSize);
      console.log('file blob from offset...', offset)
      console.log(file.slice(offset,chunkSize));
      chunk++;
  }
like image 626
Nico Galluzzo Avatar asked Oct 01 '15 22:10

Nico Galluzzo


People also ask

How do I chunk a file in JavaScript?

slice(offset, offset + chunkSize) instead of file. slice(offset, chunkSize) . Also you're producing an empty blob at the end, because your while condition should be while (chunk < chunks) . Yeah I updated the codepen with the solution below, working great.

What is a chunk in JavaScript?

JavaScript: Chunk an array into smaller arrays of a specified size.

How do I upload a file to chunks?

To enable the chunk upload, set the size to chunkSize option of the upload and it receives the value in bytes . The chunk upload functionality separates the selected files into blobs of the data or chunks. These chunks are transmitted to the server using an AJAX request.

What is video chunk file?

A video segment (or chunk) is a fragment of video information that is a collection of video frames. Combined together, these segments make up a whole video. In streaming, video segments vary in size. Understanding the size of the video segments in a stream can help you determine the most efficient segment size.


2 Answers

Was slicing off the wrong ends:

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

should have been

console.log(file.slice(offset,offset+chunkSize));
like image 173
Nico Galluzzo Avatar answered Sep 19 '22 14:09

Nico Galluzzo


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;
  }
like image 44
Muhotasim Fuad Avatar answered Sep 21 '22 14:09

Muhotasim Fuad