Skip to content

Instantly share code, notes, and snippets.

@pietrovismara
Created July 8, 2016 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietrovismara/bdf4848e1d126846900b6c648d3a6c36 to your computer and use it in GitHub Desktop.
Save pietrovismara/bdf4848e1d126846900b6c648d3a6c36 to your computer and use it in GitHub Desktop.
Slice a File/Blob instance in the desired amount of chunks
/**
* @param {File|Blob} - file to slice
* @param {Number} - chunksAmount
* @return {Array} - an array of Blobs
**/
function sliceFile(file, chunksAmount) {
var byteIndex = 0;
var chunks = [];
for (var i = 0; i < chunksAmount; i += 1) {
var byteEnd = Math.ceil((file.size / chunksAmount) * (i + 1));
chunks.push(file.slice(byteIndex, byteEnd));
byteIndex += (byteEnd - byteIndex);
}
return chunks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment