Skip to content

Commit

Permalink
Incorrect file loaded reporting (#1007)
Browse files Browse the repository at this point in the history
As the upload is completed with request.onprogress the server returns the Content-Length and the browser takes this size... In my case the returned Content-Length is lower, because i don't return the image and so the progress bar is going back.

To fix this behavier we should replace the file.loaded set in request.onprogress to:
file.loaded = Math.max(evt.loaded, file.loaded); instead of file.loaded = evt.loaded;
  • Loading branch information
mkszepp authored Nov 2, 2023
1 parent 119206a commit 7fa498c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ember-file-upload/src/system/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ export function upload(
if (!evt) return;
if (!evt.lengthComputable || evt.total === 0) return;

file.loaded = evt.loaded;
// 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.
// if there is lower, we want to keep the higher loaded value, otherwise the progress percentage will be decreased
// When the evt.loaded is higher than the start file.size, we use the file.size, otherwise it can occur that progress for the file is higher than 100%
let loaded = evt.loaded;
if (loaded > file.size) {
loaded = file.size;
}
file.loaded = Math.max(loaded, file.loaded);
file.progress = (file.loaded / file.size) * 100;
};

Expand Down

0 comments on commit 7fa498c

Please sign in to comment.