how to split a file in chunk in IOS or Javascript

How to split a file in chunk in IOS or Javascript

Uploading in HTML has always left much to be desired from developers.
Uploading files which are only a few hundred kilobytes in one go is all fine and good on a standard connection, but what of mobile uploading? Or video files in excess of several gigabytes? As upload duration increases, so too does the possibility of failure. In the case of a failed upload, the user has no choice but to restart the upload from the very first byte. Not only does this cause aggravation for your users, but it also wastes resources on your upload machines. Chunked uploading by contrast, allows us to break a large file into small chunks, and send these pieces to the upload server one-by-one. If an upload fails, we need only resume from the last successful chunk.


NSData* myBlob;
NSUInteger length = [myBlob length];
NSUInteger chunkSize = 100 * 1024;
NSUInteger offset = 0;
do {
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[myBlob bytes] + offset
offset += thisChunkSize;
// do something with chunk
} while (offset < length);


function ChunkedUploader(file, options) {
if (!this instanceof ChunkedUploader) {
return new ChunkedUploader(file, options);

this.file = file;

this.options = $.extend({
url: '/upload'
}, options);

this.file_size = this.file.size;
this.chunk_size = (1024 * 100); // 100KB
this.range_start = 0;
this.range_end = this.chunk_size;

if ('mozSlice' in this.file) 
this.slice_method = 'mozSlice';
else if ('webkitSlice' in this.file) 
this.slice_method = 'webkitSlice';
this.slice_method = 'slice';

this.upload_request = new XMLHttpRequest();
this.upload_request.onload = this._onChunkComplete;