Skip to content

Instantly share code, notes, and snippets.

@savroff
Last active March 3, 2017 15:21
Show Gist options
  • Save savroff/1c8593fdcd3327748613 to your computer and use it in GitHub Desktop.
Save savroff/1c8593fdcd3327748613 to your computer and use it in GitHub Desktop.
Resumable uploading with plupload
// If you want to create resumable uploading for your chunks plupload uploader
// you need to go in plupload.dev.js on line 1419 in xhr.onload function
// and change this part:
offset += curChunkSize;
file.loaded = Math.min(offset, blob.size);
// to this:
offset += curChunkSize;
var serverOffset = parseInt(xhr.responseText);
if (serverOffset > offset) {
offset = serverOffset;
}
file.loaded = Math.min(offset, blob.size);
// after it you need to setup you backend server to answer correct
// current size of already uploaded info in json responce
// and now your uploader try to upload first chunk and resume uploading from needed chunk.
// Enjoy :)
@alana314
Copy link

alana314 commented Dec 1, 2015

Thanks for this! This got me started. My server was returning a JSON object, not just an offset amount, so I modified this slightly, returning a chunkid from the server instead of offset:

var resp = JSON.parse(xhr.responseText);
serverOffset = parseInt(resp.chunkid * chunkSize);

if(serverOffset == 0)
    offset += curChunkSize;
else if (serverOffset > offset) {
    offset = serverOffset;
}else
{
    offset = null;
}

I also set the offset to null in certain cases because that triggers the upload being complete, and without it my transfers were sometimes hanging at the end.

@roboticsexpert
Copy link

hi .
i think i need a server side example for you solution. i did every thing , but for resuming i have problem...i use php for my server side but i know ruby... can you show me your server side code ? @savroff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment