1.0.9 • Published 11 months ago
@jswork/file-chunker v1.0.9
file-chunker
Efficiently chunk files and process them in parallel streams.
installation
npm install @jswork/file-chunkerusage
import FileChunk from '@jswork/file-chunker';
const ipt1 = document.getElementById('file');
const apiPut = ({ chunk }) => {
return fetch('https://httpbin.org/post', {
method: 'POST',
body: chunk,
headers: {
'Content-Type': 'application/octet-stream'
}
}).then(r => r.json());
};
ipt1.addEventListener('change', function(e) {
const files = e.target.files;
if (files.length) {
const file = files[0];
const chunker = new FileChunk(file, {
chunkSize: 1024 * 1024,
concurrency: 3
});
console.log(chunker.chunkCount);
chunker.processChunks(({ chunk, current, count }) => {
console.log('chunk:', chunk, 'current:', current, 'count:', count, 'percent: ', (current / count * 100).toFixed(2) + '%');
return apiPut({ chunk });
}).then(res => {
console.log('res: ', res);
});
}
});license
Code released under the MIT license.