Skip to content

Commit

Permalink
Added official ChunkUploaded event, it similar to FileUploaded but ex…
Browse files Browse the repository at this point in the history
…ecuted for each chunk.
  • Loading branch information
spocke committed Mar 1, 2010
1 parent fab5d57 commit b1ba46a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 1.x (2010-xx-x)
Added official ChunkUploaded event, it similar to FileUploaded but executed for each chunk.
Added bytes per second support to total queue progress.
Added better error handling to core API using the new Error event.
Added better error handling to jQuery queue widget.
Expand Down
13 changes: 11 additions & 2 deletions src/javascript/plupload.browserplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,22 @@
up.trigger('UploadProgress', file);
}
}, function(res) {
var httpStatus;
var httpStatus, chunkArgs;

if (res.success) {
httpStatus = res.value.statusCode;

if (chunkSize) {
up.trigger('ChunkUploaded', file, {
chunk : chunk,
chunks : chunks,
response : res.value.body,
status : httpStatus
});
}

if (chunkStack.length > 0) {
// more chunks to be uploaded
// More chunks to be uploaded
uploadFile(++chunk, chunks);
} else {
file.status = plupload.DONE;
Expand Down
5 changes: 2 additions & 3 deletions src/javascript/plupload.flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,15 @@
var chunkArgs, file = up.getFile(lookup[info.id]);

chunkArgs = {
file : file,
chunk : info.chunk,
chunks : info.chunks,
response : info.text
};

up.trigger('ChunkUploaded', chunkArgs);
up.trigger('ChunkUploaded', file, chunkArgs);

// Stop upload if file is maked as failed
if (chunkArgs.cancelled) {
if (file.status == plupload.FAILED) {
getFlashObj().cancelUpload();
}
});
Expand Down
15 changes: 11 additions & 4 deletions src/javascript/plupload.gears.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@
if (req.readyState == 4) {
if (req.status == 200) {
chunkArgs = {
file : file,
chunk : chunk,
chunks : chunks,
response : req.responseText
response : req.responseText,
status : req.status
};

up.trigger('ChunkUploaded', chunkArgs);
up.trigger('ChunkUploaded', file, chunkArgs);

// Stop upload
if (chunkArgs.cancelled) {
Expand All @@ -249,7 +249,14 @@
uploadNextChunk();
}
} else {
up.trigger('UploadChunkError', {file : file, chunk : chunk, chunks : chunks, error : 'Status: ' + req.status});
up.trigger('Error', {
code : plupload.HTTP_ERROR,
message : 'HTTP Error.',
file : file,
chunk : chunk,
chunks : chunks,
status : req.status
});
}
}
};
Expand Down
9 changes: 9 additions & 0 deletions src/javascript/plupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,15 @@
* @param {plupload.File} file File that was uploaded.
* @param {Object} response Object with response properties.
*/

/**
* Fires when file chunk is uploaded.
*
* @event ChunkUploaded
* @param {plupload.Uploader} uploader Uploader instance sending the event.
* @param {plupload.File} file File that the chunk was uploaded for.
* @param {Object} response Object with response properties.
*/
});
};

Expand Down
3 changes: 1 addition & 2 deletions src/javascript/plupload.silverlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,12 @@
var chunkArgs, file = up.getFile(lookup[sl_id]);

chunkArgs = {
file : file,
chunk : chunk,
chunks : chunks,
response : text
};

up.trigger('ChunkUploaded', chunkArgs);
up.trigger('ChunkUploaded', file, chunkArgs);

// Stop upload if file is maked as failed
if (file.status == plupload.FAILED) {
Expand Down
17 changes: 14 additions & 3 deletions tests/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@
console.log('Error', err);
});

uploader.bind('FileUploaded', function(up, file, response) {
function handleResponse(up, file, response) {
var code = parseInt($('#errorcode').val(), 10), err = {code : code};

console.log('FileUploaded', file, response);
if (response.chunks) {
console.log('ChunkUploaded', file, response);
} else {
console.log('FileUploaded', file, response);
}

switch (code) {
case plupload.GENERIC_ERROR:
Expand Down Expand Up @@ -160,14 +164,20 @@

break;

case 0:
return;

default:
err.message = 'User specific error.';
err.details = 'Some custom data.';
err.file = file;
}

uploader.trigger('Error', err);
});
};

uploader.bind('ChunkUploaded', handleResponse);
uploader.bind('FileUploaded', handleResponse);
};
});
</script>
Expand All @@ -180,6 +190,7 @@ <h1>Multipart test page</h1>
<div>
Error to fake:
<select id="errorcode">
<option value="0">No error</option>
<option value="-100">GENERIC_ERROR</option>
<option value="-200">HTTP_ERROR</option>
<option value="-300">IO_ERROR</option>
Expand Down

0 comments on commit b1ba46a

Please sign in to comment.