Skip to content

Commit

Permalink
Fix upload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mkszepp committed Nov 8, 2023
1 parent c3af01e commit 4113e2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ember-file-upload/src/system/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ export function upload(
if (!evt.lengthComputable || evt.total === 0) return;

file.loaded = evt.loaded;
file.size = evt.total;
// It occurs that the evt.total is not always correct.
// For this reason we should hold the max file size.
// The correct should be returned while progress
file.size = Math.max(file.size, evt.total);
file.progress = (file.loaded / file.size) * 100;
};

request.onprogress = function (evt) {
if (!evt) return;
if (!evt.lengthComputable || evt.total === 0) return;
// We need to check also for isUploadComplete, because the browsers brings sometimes the onprogress after onloadend event
if (!evt.lengthComputable || evt.total === 0 || file.isUploadComplete)
return;

file.size = evt.total;

// When the progress is completed there is possible that we get the `Content-Length` response header of the upload endpoint as loaded / total.
// There is possible that `Content-Length` is lower or higher than the already loaded bytes.
Expand All @@ -118,6 +125,7 @@ export function upload(

file.loaded = file.size;
file.progress = (file.loaded / file.size) * 100;
file.isUploadComplete = true;
};

request.ontimeout = () => {
Expand Down
5 changes: 5 additions & 0 deletions ember-file-upload/src/upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export class UploadFile {
* range of 0 to 100.
*/
@tracked progress = 0;

/**
* When upload has finished this property will be set to true
*/
@tracked isUploadComplete = false;

/**
* The current state that the file is in.
Expand Down

0 comments on commit 4113e2a

Please sign in to comment.