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 :)
@toutavous
Copy link

Hi, Savroff, I am trying to create a resumable file upload using plupload. Please, I would like to see a sample of the php server code you used to achieve resumable upload in a disconnected upload scenario

@savroff
Copy link
Author

savroff commented Oct 26, 2015

@toutavous Sorry I'm use ruby server. If someone disconnected, you check last uploaded chunk number (in folder, in database it's up to you) and in first chunk uploading request return chunk number + 1. This mean you return chunk number for resuming js put it in serverOffset and uploading move to this chunk

@savroff
Copy link
Author

savroff commented Oct 26, 2015

@Setitch after first request you can return chunk numbers for example: { last_chunk_number: 14, missed_chunk_numbers: [ 2, 3 ] }. After it you can control in javascript what chunk you need to send (in this case 2 and 3) and after start from chunk number 15.

@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