Created
July 8, 2016 11:38
-
-
Save pietrovismara/bdf4848e1d126846900b6c648d3a6c36 to your computer and use it in GitHub Desktop.
Slice a File/Blob instance in the desired amount of chunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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